build.gradle (Project: MyApplication)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() maven { url 'https://jitpack.io' } } } task clean(type: Delete) { delete rootProject.buildDir } |
build.gradle (Module: app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:26.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.iceteck.silicompressorr:silicompressor:2.0' implementation 'com.github.vansikrishna:Multimager:1.0.6' implementation 'com.snatik:storage:2.1.0' } |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.user.tn_silicompressor.MainActivity" tools:showIn="@layout/activity_main"> <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="1"> <Button android:id="@+id/bt_select" android:layout_gravity="center_horizontal" android:layout_marginBottom="20dp" android:text="Select Image" /> <TextView android:id="@+id/text_src" android:layout_gravity="center_horizontal" android:layout_marginBottom="5dp" android:text="Source Image" android:textSize="18sp" /> <ImageView android:id="@+id/img_src" android:layout_width="match_parent" android:layout_height="200dp" android:layout_gravity="center_horizontal" android:layout_marginBottom="10dp" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/text_comp" android:layout_gravity="center_horizontal" android:layout_marginBottom="5dp" android:text="Compressed Image" android:textSize="18sp" /> <ImageView android:id="@+id/img_comp" android:layout_width="match_parent" android:layout_height="200dp" android:layout_gravity="center_horizontal" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" /> </GridLayout> </ScrollView> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.widget.ImageView; import android.widget.TextView; import com.iceteck.silicompressorr.SiliCompressor; import com.snatik.storage.Storage; import com.snatik.storage.helpers.SizeUnit; import com.vlk.multimager.activities.GalleryActivity; import com.vlk.multimager.utils.Constants; import com.vlk.multimager.utils.Image; import com.vlk.multimager.utils.Params; import java.io.File; import java.util.ArrayList; import java.util.List; import java.text.DecimalFormat; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); fab.hide(); findViewById(R.id.bt_select).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onButtonClick(); } }); } private void onButtonClick() { //ให้แสดง Gallery เพื่อเลือกภาพ Intent intent = new Intent(this, GalleryActivity.class); Params params = new Params(); params.setPickerLimit(1); //ให้เลือกได้เพียงภาพเดียว intent.putExtra(Constants.KEY_PARAMS, params); startActivityForResult(intent, Constants.TYPE_MULTI_PICKER); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (resultCode != RESULT_OK) { return; } if (requestCode == Constants.TYPE_MULTI_PICKER) { ArrayList<Image> images = intent.getParcelableArrayListExtra( Constants.KEY_BUNDLE_LIST); String path = images.get(0).imagePath; //พาธของภาพที่เลือก //แสดงภาพต้นฉบับ (ยังไม่บีบอัด) ใน ImageView ImageView imgSrc = (ImageView) findViewById(R.id.img_src); imgSrc.setImageURI(Uri.parse(path)); //อ่านขนาดของไฟล์ภาพต้นฉบับ Storage storage = new Storage(this); double size = storage.getSize(new File(path), SizeUnit.MB); String strSize = DecimalFormat.getInstance().format(size); TextView textSrc = (TextView) findViewById(R.id.text_src); textSrc.setText("Source Image : (" + strSize + " MB)"); //ต่อไปจะเกี่ยวกับภาพที่บีบอัด try { //บีบอัดภาพที่เลือกมา Bitmap bmp = SiliCompressor.with(this) .getCompressBitmap(images.get(0).imagePath); //แสดงภาพที่บีบอัดแล้วใน ImageView ImageView imgComp = (ImageView) findViewById(R.id.img_comp); imgComp.setImageBitmap(bmp); //ตำแหน่งที่เก็บไฟล์สำรองของ SiliCompressor path = storage.getExternalStorageDirectory() + "/Silicompressor/images"; //อ่านรายชื่อไฟล์จากตำแหน่งที่เก็บไฟล์สำรองของ SiliCompressor List<File> files = storage.getNestedFiles(path); path = files.get(0).getPath(); //เลือกแค่ไฟล์แรก //อ่านขนาดของไฟล์สำรองเพื่อแสดงใน TextView size = storage.getSize(new File(path), SizeUnit.MB); strSize = DecimalFormat.getInstance().format(size); TextView textComp = (TextView) findViewById(R.id.text_comp); textComp.setText("Compressed Image : (" + strSize + " MB)"); //ลบไฟล์สำรองนั้นทิ้งไป storage.deleteFile(path); } catch (Exception ex) { } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |