1
0
郑梓斌 8 жил өмнө
commit
55cc4c0aeb
49 өөрчлөгдсөн 1564 нэмэгдсэн , 0 устгасан
  1. 8 0
      .gitignore
  2. 1 0
      .idea/.name
  3. 22 0
      .idea/compiler.xml
  4. 3 0
      .idea/copyright/profiles_settings.xml
  5. 7 0
      .idea/dictionaries/sweetspot.xml
  6. 6 0
      .idea/encodings.xml
  7. 25 0
      .idea/gradle.xml
  8. 62 0
      .idea/misc.xml
  9. 11 0
      .idea/modules.xml
  10. 12 0
      .idea/runConfigurations.xml
  11. 6 0
      .idea/vcs.xml
  12. 23 0
      build.gradle
  13. 1 0
      example/.gitignore
  14. 34 0
      example/build.gradle
  15. 17 0
      example/proguard-rules.pro
  16. 13 0
      example/src/androidTest/java/top/zibin/luban/example/ApplicationTest.java
  17. 33 0
      example/src/main/AndroidManifest.xml
  18. 89 0
      example/src/main/java/top/zibin/luban/example/MainActivity.java
  19. 265 0
      example/src/main/java/top/zibin/luban/example/PathUtils.java
  20. 39 0
      example/src/main/res/layout/activity_main.xml
  21. 60 0
      example/src/main/res/layout/content_main.xml
  22. BIN
      example/src/main/res/mipmap-hdpi/ic_launcher.png
  23. BIN
      example/src/main/res/mipmap-mdpi/ic_launcher.png
  24. BIN
      example/src/main/res/mipmap-xhdpi/ic_launcher.png
  25. BIN
      example/src/main/res/mipmap-xxhdpi/ic_launcher.png
  26. BIN
      example/src/main/res/mipmap-xxxhdpi/ic_launcher.png
  27. 9 0
      example/src/main/res/values-v21/styles.xml
  28. 6 0
      example/src/main/res/values-w820dp/dimens.xml
  29. 6 0
      example/src/main/res/values/colors.xml
  30. 6 0
      example/src/main/res/values/dimens.xml
  31. 4 0
      example/src/main/res/values/strings.xml
  32. 20 0
      example/src/main/res/values/styles.xml
  33. 15 0
      example/src/test/java/top/zibin/luban/example/ExampleUnitTest.java
  34. 18 0
      gradle.properties
  35. BIN
      gradle/wrapper/gradle-wrapper.jar
  36. 6 0
      gradle/wrapper/gradle-wrapper.properties
  37. 160 0
      gradlew
  38. 90 0
      gradlew.bat
  39. 1 0
      library/.gitignore
  40. 25 0
      library/build.gradle
  41. 17 0
      library/proguard-rules.pro
  42. 13 0
      library/src/androidTest/java/top/zibin/luban/ApplicationTest.java
  43. 8 0
      library/src/main/AndroidManifest.xml
  44. 359 0
      library/src/main/java/top/zibin/luban/Luban.java
  45. 7 0
      library/src/main/java/top/zibin/luban/OnCompressListener.java
  46. 38 0
      library/src/main/java/top/zibin/luban/Preconditions.java
  47. 3 0
      library/src/main/res/values/strings.xml
  48. 15 0
      library/src/test/java/top/zibin/luban/ExampleUnitTest.java
  49. 1 0
      settings.gradle

+ 8 - 0
.gitignore

@@ -0,0 +1,8 @@
+*.iml
+.gradle
+/local.properties
+/.idea/workspace.xml
+/.idea/libraries
+.DS_Store
+/build
+/captures

+ 1 - 0
.idea/.name

@@ -0,0 +1 @@
+Luban

+ 22 - 0
.idea/compiler.xml

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="CompilerConfiguration">
+    <resourceExtensions />
+    <wildcardResourcePatterns>
+      <entry name="!?*.java" />
+      <entry name="!?*.form" />
+      <entry name="!?*.class" />
+      <entry name="!?*.groovy" />
+      <entry name="!?*.scala" />
+      <entry name="!?*.flex" />
+      <entry name="!?*.kt" />
+      <entry name="!?*.clj" />
+      <entry name="!?*.aj" />
+    </wildcardResourcePatterns>
+    <annotationProcessing>
+      <profile default="true" name="Default" enabled="false">
+        <processorPath useClasspath="true" />
+      </profile>
+    </annotationProcessing>
+  </component>
+</project>

+ 3 - 0
.idea/copyright/profiles_settings.xml

@@ -0,0 +1,3 @@
+<component name="CopyrightManager">
+  <settings default="" />
+</component>

+ 7 - 0
.idea/dictionaries/sweetspot.xml

@@ -0,0 +1,7 @@
+<component name="ProjectDictionaryState">
+  <dictionary name="sweetspot">
+    <words>
+      <w>luban</w>
+    </words>
+  </dictionary>
+</component>

+ 6 - 0
.idea/encodings.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding">
+    <file url="PROJECT" charset="UTF-8" />
+  </component>
+</project>

+ 25 - 0
.idea/gradle.xml

@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="GradleSettings">
+    <option name="linkedExternalProjectsSettings">
+      <GradleProjectSettings>
+        <option name="distributionType" value="DEFAULT_WRAPPED" />
+        <option name="externalProjectPath" value="$PROJECT_DIR$" />
+        <option name="modules">
+          <set>
+            <option value="$PROJECT_DIR$" />
+            <option value="$PROJECT_DIR$/example" />
+            <option value="$PROJECT_DIR$/library" />
+          </set>
+        </option>
+        <option name="myModules">
+          <set>
+            <option value="$PROJECT_DIR$" />
+            <option value="$PROJECT_DIR$/example" />
+            <option value="$PROJECT_DIR$/library" />
+          </set>
+        </option>
+      </GradleProjectSettings>
+    </option>
+  </component>
+</project>

+ 62 - 0
.idea/misc.xml

@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="EntryPointsManager">
+    <entry_points version="2.0" />
+  </component>
+  <component name="NullableNotNullManager">
+    <option name="myDefaultNullable" value="android.support.annotation.Nullable" />
+    <option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
+    <option name="myNullables">
+      <value>
+        <list size="4">
+          <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
+          <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
+          <item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
+          <item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
+        </list>
+      </value>
+    </option>
+    <option name="myNotNulls">
+      <value>
+        <list size="4">
+          <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
+          <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
+          <item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
+          <item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
+        </list>
+      </value>
+    </option>
+  </component>
+  <component name="ProjectLevelVcsManager" settingsEditedManually="false">
+    <OptionsSetting value="true" id="Add" />
+    <OptionsSetting value="true" id="Remove" />
+    <OptionsSetting value="true" id="Checkout" />
+    <OptionsSetting value="true" id="Update" />
+    <OptionsSetting value="true" id="Status" />
+    <OptionsSetting value="true" id="Edit" />
+    <ConfirmationsSetting value="0" id="Add" />
+    <ConfirmationsSetting value="0" id="Remove" />
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/build/classes" />
+  </component>
+  <component name="ProjectType">
+    <option name="id" value="Android" />
+  </component>
+  <component name="masterDetails">
+    <states>
+      <state key="ProjectJDKs.UI">
+        <settings>
+          <last-edited>1.8</last-edited>
+          <splitter-proportions>
+            <option name="proportions">
+              <list>
+                <option value="0.2" />
+              </list>
+            </option>
+          </splitter-proportions>
+        </settings>
+      </state>
+    </states>
+  </component>
+</project>

+ 11 - 0
.idea/modules.xml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/Luban.iml" filepath="$PROJECT_DIR$/Luban.iml" />
+      <module fileurl="file://$PROJECT_DIR$/example/app.iml" filepath="$PROJECT_DIR$/example/app.iml" />
+      <module fileurl="file://$PROJECT_DIR$/example/example.iml" filepath="$PROJECT_DIR$/example/example.iml" />
+      <module fileurl="file://$PROJECT_DIR$/library/library.iml" filepath="$PROJECT_DIR$/library/library.iml" />
+    </modules>
+  </component>
+</project>

+ 12 - 0
.idea/runConfigurations.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="RunConfigurationProducerService">
+    <option name="ignoredProducers">
+      <set>
+        <option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" />
+        <option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" />
+        <option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" />
+      </set>
+    </option>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 23 - 0
build.gradle

@@ -0,0 +1,23 @@
+// Top-level build file where you can add configuration options common to all sub-projects/modules.
+
+buildscript {
+    repositories {
+        jcenter()
+    }
+    dependencies {
+        classpath 'com.android.tools.build:gradle:2.1.2'
+
+        // NOTE: Do not place your application dependencies here; they belong
+        // in the individual module build.gradle files
+    }
+}
+
+allprojects {
+    repositories {
+        jcenter()
+    }
+}
+
+task clean(type: Delete) {
+    delete rootProject.buildDir
+}

+ 1 - 0
example/.gitignore

@@ -0,0 +1 @@
+/build

+ 34 - 0
example/build.gradle

@@ -0,0 +1,34 @@
+apply plugin: 'com.android.application'
+
+android {
+    compileSdkVersion 23
+    buildToolsVersion "23.0.3"
+
+    defaultConfig {
+        applicationId "top.zibin.luban.example"
+        minSdkVersion 11
+        targetSdkVersion 23
+        versionCode 1
+        versionName "1.0"
+    }
+    buildTypes {
+        release {
+            minifyEnabled false
+            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+        }
+    }
+}
+
+dependencies {
+    compile fileTree(include: ['*.jar'], dir: 'libs')
+    testCompile 'junit:junit:4.12'
+    compile 'com.android.support:appcompat-v7:23.4.0'
+    compile 'com.android.support:design:23.4.0'
+    compile 'io.reactivex:rxandroid:1.1.0'
+    compile 'io.reactivex:rxjava:1.1.3'
+
+    compile 'com.nineoldandroids:library:2.4.0'
+    compile 'com.github.bumptech.glide:glide:3.7.0'
+    compile 'me.iwf.photopicker:PhotoPicker:0.8.5@aar'
+    compile project(':library')
+}

+ 17 - 0
example/proguard-rules.pro

@@ -0,0 +1,17 @@
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in /Users/sweetspot/Library/Android/sdk/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the proguardFiles
+# directive in build.gradle.
+#
+# For more details, see
+#   http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+#   public *;
+#}

+ 13 - 0
example/src/androidTest/java/top/zibin/luban/example/ApplicationTest.java

@@ -0,0 +1,13 @@
+package top.zibin.luban.example;
+
+import android.app.Application;
+import android.test.ApplicationTestCase;
+
+/**
+ * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
+ */
+public class ApplicationTest extends ApplicationTestCase<Application> {
+    public ApplicationTest() {
+        super(Application.class);
+    }
+}

+ 33 - 0
example/src/main/AndroidManifest.xml

@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="top.zibin.luban.example">
+
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+
+    <application
+        android:allowBackup="true"
+        android:icon="@mipmap/ic_launcher"
+        android:label="@string/app_name"
+        android:supportsRtl="true"
+        android:theme="@style/AppTheme">
+        <activity
+            android:name=".MainActivity"
+            android:label="@string/title_activity_main"
+            android:theme="@style/AppTheme.NoActionBar">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+
+        <activity
+            android:name="me.iwf.photopicker.PhotoPickerActivity"
+            android:theme="@style/Theme.AppCompat.NoActionBar" />
+
+        <activity
+            android:name="me.iwf.photopicker.PhotoPagerActivity"
+            android:theme="@style/Theme.AppCompat.NoActionBar" />
+    </application>
+
+</manifest>

+ 89 - 0
example/src/main/java/top/zibin/luban/example/MainActivity.java

@@ -0,0 +1,89 @@
+package top.zibin.luban.example;
+
+import android.annotation.TargetApi;
+import android.content.ContentUris;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Build;
+import android.os.Bundle;
+import android.os.Environment;
+import android.provider.DocumentsContract;
+import android.provider.MediaStore;
+import android.support.design.widget.FloatingActionButton;
+import android.support.v7.app.AppCompatActivity;
+import android.support.v7.widget.Toolbar;
+import android.view.View;
+import android.widget.TextView;
+
+import java.io.File;
+import java.util.ArrayList;
+
+import me.iwf.photopicker.PhotoPicker;
+import top.zibin.luban.Luban;
+import top.zibin.luban.OnCompressListener;
+
+public class MainActivity extends AppCompatActivity {
+    private final int CODE = 1;
+    private Uri imgUri;
+
+    private TextView fileSize;
+    private TextView imageSize;
+    private TextView thumbFileSize;
+    private TextView thumbImageSize;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_main);
+        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
+        setSupportActionBar(toolbar);
+
+        fileSize = (TextView) findViewById(R.id.file_size);
+        imageSize = (TextView) findViewById(R.id.image_size);
+        thumbFileSize = (TextView) findViewById(R.id.thumb_file_size);
+        thumbImageSize = (TextView) findViewById(R.id.thumb_image_size);
+
+        imgUri = Uri.fromFile(Luban.getPhotoCacheDir(getApplicationContext(), getString(R.string.app_name)));
+
+        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
+        fab.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View view) {
+                PhotoPicker.builder()
+                        .setPhotoCount(1)
+                        .setShowCamera(true)
+                        .setShowGif(true)
+                        .setPreviewEnabled(false)
+                        .start(MainActivity.this, PhotoPicker.REQUEST_CODE);
+
+            }
+        });
+    }
+
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+
+        if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
+            if (data != null) {
+                ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
+
+                File imgFile = new File(photos.get(0));
+                fileSize.setText(imgFile.length() / 1024 + "k");
+                imageSize.setText(Luban.get(this).getImageSize(imgFile.getPath())[0] + " * " + Luban.get(this).getImageSize(imgFile.getPath())[1]);
+
+                Luban.get(this)
+                        .load(new File(photos.get(0)))
+                        .putGear(Luban.THIRD_GEAR)
+                        .setCompressListener(new OnCompressListener() {
+                            @Override
+                            public void onSuccess(File file) {
+                                thumbFileSize.setText(file.length() / 1024 + "k");
+                                thumbImageSize.setText(Luban.get(getApplicationContext()).getImageSize(file.getPath())[0] + " * " + Luban.get(getApplicationContext()).getImageSize(file.getPath())[1]);
+                            }
+                        }).launch();
+            }
+        }
+    }
+}

+ 265 - 0
example/src/main/java/top/zibin/luban/example/PathUtils.java

@@ -0,0 +1,265 @@
+package top.zibin.luban.example;
+
+import android.annotation.TargetApi;
+import android.content.ContentUris;
+import android.content.Context;
+import android.content.CursorLoader;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Build;
+import android.os.Environment;
+import android.provider.DocumentsContract;
+import android.provider.MediaStore;
+
+import java.io.File;
+
+/**
+ * <b>ClassName</b> PathUtils.java<br>
+ * <p/>
+ * <b>BuildTime:</b> 2014-9-7<br>
+ * <b>Author:</b> Curzbin<br>
+ * <p/>
+ * <b>UpdateTime:</b> <br>
+ * <b>UpdateUser:</b> <br>
+ * <p/>
+ * <b>Description:</b> The auxiliary class of Path<br>
+ */
+public class PathUtils {
+
+    public final static String SDCARD_MNT = "/mnt/sdcard";
+    public final static String SDCARD = Environment.getExternalStorageDirectory().getPath();
+
+    /**
+     * <b>BuildTime:</b> 2014-10-22<br>
+     * <b>Description:</b> get SDCard path<br>
+     *
+     * @return String of path
+     */
+    public static String getSDCardPath() {
+        return Environment.getExternalStorageDirectory().getPath();
+    }
+
+    /**
+     * <b>BuildTime:</b> 2014年10月23日<br>
+     * <b>Description:</b> <br>
+     *
+     * @param mUri
+     * @return
+     */
+    public static String getAbsolutePathFromNoStandardUri(Uri mUri) {
+        String filePath = null;
+
+        String mUriString = mUri.toString();
+        mUriString = Uri.decode(mUriString);
+
+        String pre1 = "file://" + SDCARD + File.separator;
+        String pre2 = "file://" + SDCARD_MNT + File.separator;
+
+        if (mUriString.startsWith(pre1)) {
+            filePath = Environment.getExternalStorageDirectory().getPath()
+                    + File.separator + mUriString.substring(pre1.length());
+        } else if (mUriString.startsWith(pre2)) {
+            filePath = Environment.getExternalStorageDirectory().getPath()
+                    + File.separator + mUriString.substring(pre2.length());
+        }
+        return filePath;
+    }
+
+    /**
+     * <b>BuildTime:</b> 2014年10月23日<br>
+     * <b>Description:</b> Use the uri to get the file path<br>
+     *
+     * @param c
+     * @param uri
+     * @return
+     */
+    public static String getAbsoluteUriPath(Context c, Uri uri) {
+        String imgPath = "";
+        String[] proj = {MediaStore.Images.Media.DATA};
+        Cursor cursor = new CursorLoader(c, uri, proj, null, null, null).loadInBackground();
+
+        if (cursor != null) {
+            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
+            if (cursor.getCount() > 0 && cursor.moveToFirst()) {
+                imgPath = cursor.getString(column_index);
+            }
+        }
+
+        return imgPath;
+    }
+
+    /**
+     * <b>BuildTime:</b> 2014-8-30<br>
+     * <b>Description:</b> Get the external cache directory,it will be bulid a
+     * directory what is name "Android/data/PACKAGE_NAME/cache" for 2.2 system"<br>
+     *
+     * @param context
+     * @return
+     */
+    public static File getExternalCacheDir(Context context) {
+        if (hasExternalCacheDir()) {
+            return context.getExternalCacheDir();
+        }
+
+        final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
+        return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
+    }
+
+    /**
+     * <b>BuildTime:</b> 2014-8-30<br>
+     * <b>Description:</b> Check directory,if null,create it<br>
+     *
+     * @param parent
+     * @param dirName
+     * @return
+     */
+    public static File findOrCreateDir(File parent, String dirName) {
+        File directory = new File(parent, dirName);
+        if (!directory.exists()) {
+            directory.mkdirs();
+        }
+        return directory;
+    }
+
+    private static boolean hasExternalCacheDir() {
+        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
+    }
+
+    @TargetApi(Build.VERSION_CODES.KITKAT)
+    public static String getPath(final Context context, final Uri uri) {
+
+        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
+
+        // DocumentProvider
+        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
+            // ExternalStorageProvider
+            if (isExternalStorageDocument(uri)) {
+                final String docId = DocumentsContract.getDocumentId(uri);
+                final String[] split = docId.split(":");
+                final String type = split[0];
+
+                if ("primary".equalsIgnoreCase(type)) {
+                    return Environment.getExternalStorageDirectory() + "/" + split[1];
+                }
+
+                // TODO handle non-primary volumes
+            }
+            // DownloadsProvider
+            else if (isDownloadsDocument(uri)) {
+
+                final String id = DocumentsContract.getDocumentId(uri);
+                final Uri contentUri = ContentUris.withAppendedId(
+                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
+
+                return getDataColumn(context, contentUri, null, null);
+            }
+            // MediaProvider
+            else if (isMediaDocument(uri)) {
+                final String docId = DocumentsContract.getDocumentId(uri);
+                final String[] split = docId.split(":");
+                final String type = split[0];
+
+                Uri contentUri = null;
+                switch (type) {
+                    case "image":
+                        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
+                        break;
+                    case "video":
+                        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
+                        break;
+                    case "audio":
+                        contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
+                        break;
+                }
+
+                final String selection = "_id=?";
+                final String[] selectionArgs = new String[]{
+                        split[1]
+                };
+
+                return getDataColumn(context, contentUri, selection, selectionArgs);
+            }
+        }
+        // MediaStore (and general)
+        else if ("content".equalsIgnoreCase(uri.getScheme())) {
+
+            // Return the remote address
+            if (isGooglePhotosUri(uri))
+                return uri.getLastPathSegment();
+
+            return getDataColumn(context, uri, null, null);
+        }
+        // File
+        else if ("file".equalsIgnoreCase(uri.getScheme())) {
+            return uri.getPath();
+        }
+
+        return null;
+    }
+
+    /**
+     * Get the value of the data column for this Uri. This is useful for
+     * MediaStore Uris, and other file-based ContentProviders.
+     *
+     * @param context       The context.
+     * @param uri           The Uri to query.
+     * @param selection     (Optional) Filter used in the query.
+     * @param selectionArgs (Optional) Selection arguments used in the query.
+     * @return The value of the _data column, which is typically a file path.
+     */
+    public static String getDataColumn(Context context, Uri uri, String selection,
+                                       String[] selectionArgs) {
+
+        Cursor cursor = null;
+        final String column = "_data";
+        final String[] projection = {
+                column
+        };
+
+        try {
+            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
+                    null);
+            if (cursor != null && cursor.moveToFirst()) {
+                final int index = cursor.getColumnIndexOrThrow(column);
+                return cursor.getString(index);
+            }
+        } finally {
+            if (cursor != null)
+                cursor.close();
+        }
+        return null;
+    }
+
+
+    /**
+     * @param uri The Uri to check.
+     * @return Whether the Uri authority is ExternalStorageProvider.
+     */
+    public static boolean isExternalStorageDocument(Uri uri) {
+        return "com.android.externalstorage.documents".equals(uri.getAuthority());
+    }
+
+    /**
+     * @param uri The Uri to check.
+     * @return Whether the Uri authority is DownloadsProvider.
+     */
+    public static boolean isDownloadsDocument(Uri uri) {
+        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
+    }
+
+    /**
+     * @param uri The Uri to check.
+     * @return Whether the Uri authority is MediaProvider.
+     */
+    public static boolean isMediaDocument(Uri uri) {
+        return "com.android.providers.media.documents".equals(uri.getAuthority());
+    }
+
+    /**
+     * @param uri The Uri to check.
+     * @return Whether the Uri authority is Google Photos.
+     */
+    public static boolean isGooglePhotosUri(Uri uri) {
+        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
+    }
+}

+ 39 - 0
example/src/main/res/layout/activity_main.xml

@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true"
+    android:orientation="vertical">
+
+    <android.support.design.widget.AppBarLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:theme="@style/AppTheme.AppBarOverlay">
+
+        <android.support.v7.widget.Toolbar
+            android:id="@+id/toolbar"
+            android:layout_width="match_parent"
+            android:layout_height="?attr/actionBarSize"
+            android:background="?attr/colorPrimary"
+            app:popupTheme="@style/AppTheme.PopupOverlay" />
+
+    </android.support.design.widget.AppBarLayout>
+
+    <RelativeLayout
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+
+        <include layout="@layout/content_main" />
+
+        <android.support.design.widget.FloatingActionButton
+            android:id="@+id/fab"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_alignParentBottom="true"
+            android:layout_alignParentEnd="true"
+            android:layout_alignParentRight="true"
+            android:layout_margin="@dimen/fab_margin"
+            android:src="@android:drawable/ic_dialog_email" />
+    </RelativeLayout>
+</LinearLayout>

+ 60 - 0
example/src/main/res/layout/content_main.xml

@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout 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:orientation="vertical"
+    android:padding="@dimen/activity_vertical_margin"
+    app:layout_behavior="@string/appbar_scrolling_view_behavior"
+    tools:context="top.zibin.luban.example.MainActivity"
+    tools:showIn="@layout/activity_main">
+
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:text="原图参数" />
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal">
+
+        <TextView
+            android:id="@+id/file_size"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="1" />
+
+        <TextView
+            android:id="@+id/image_size"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="1" />
+    </LinearLayout>
+
+
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:text="压缩后参数" />
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal">
+
+        <TextView
+            android:id="@+id/thumb_file_size"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="1" />
+
+        <TextView
+            android:id="@+id/thumb_image_size"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="1" />
+    </LinearLayout>
+
+</LinearLayout>

BIN
example/src/main/res/mipmap-hdpi/ic_launcher.png


BIN
example/src/main/res/mipmap-mdpi/ic_launcher.png


BIN
example/src/main/res/mipmap-xhdpi/ic_launcher.png


BIN
example/src/main/res/mipmap-xxhdpi/ic_launcher.png


BIN
example/src/main/res/mipmap-xxxhdpi/ic_launcher.png


+ 9 - 0
example/src/main/res/values-v21/styles.xml

@@ -0,0 +1,9 @@
+<resources>
+
+    <style name="AppTheme.NoActionBar">
+        <item name="windowActionBar">false</item>
+        <item name="windowNoTitle">true</item>
+        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
+        <item name="android:statusBarColor">@android:color/transparent</item>
+    </style>
+</resources>

+ 6 - 0
example/src/main/res/values-w820dp/dimens.xml

@@ -0,0 +1,6 @@
+<resources>
+    <!-- Example customization of dimensions originally defined in res/values/dimens.xml
+         (such as screen margins) for screens with more than 820dp of available width. This
+         would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
+    <dimen name="activity_horizontal_margin">64dp</dimen>
+</resources>

+ 6 - 0
example/src/main/res/values/colors.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <color name="colorPrimary">#3F51B5</color>
+    <color name="colorPrimaryDark">#303F9F</color>
+    <color name="colorAccent">#FF4081</color>
+</resources>

+ 6 - 0
example/src/main/res/values/dimens.xml

@@ -0,0 +1,6 @@
+<resources>
+    <!-- Default screen margins, per the Android Design guidelines. -->
+    <dimen name="activity_horizontal_margin">16dp</dimen>
+    <dimen name="activity_vertical_margin">16dp</dimen>
+    <dimen name="fab_margin">16dp</dimen>
+</resources>

+ 4 - 0
example/src/main/res/values/strings.xml

@@ -0,0 +1,4 @@
+<resources>
+    <string name="app_name">Luban</string>
+    <string name="title_activity_main">MainActivity</string>
+</resources>

+ 20 - 0
example/src/main/res/values/styles.xml

@@ -0,0 +1,20 @@
+<resources>
+
+    <!-- Base application theme. -->
+    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
+        <!-- Customize your theme here. -->
+        <item name="colorPrimary">@color/colorPrimary</item>
+        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
+        <item name="colorAccent">@color/colorAccent</item>
+    </style>
+
+    <style name="AppTheme.NoActionBar">
+        <item name="windowActionBar">false</item>
+        <item name="windowNoTitle">true</item>
+    </style>
+
+    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
+
+    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
+
+</resources>

+ 15 - 0
example/src/test/java/top/zibin/luban/example/ExampleUnitTest.java

@@ -0,0 +1,15 @@
+package top.zibin.luban.example;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * To work on unit tests, switch the Test Artifact in the Build Variants view.
+ */
+public class ExampleUnitTest {
+    @Test
+    public void addition_isCorrect() throws Exception {
+        assertEquals(4, 2 + 2);
+    }
+}

+ 18 - 0
gradle.properties

@@ -0,0 +1,18 @@
+# Project-wide Gradle settings.
+
+# IDE (e.g. Android Studio) users:
+# Gradle settings configured through the IDE *will override*
+# any settings specified in this file.
+
+# For more details on how to configure your build environment visit
+# http://www.gradle.org/docs/current/userguide/build_environment.html
+
+# Specifies the JVM arguments used for the daemon process.
+# The setting is particularly useful for tweaking memory settings.
+# Default value: -Xmx10248m -XX:MaxPermSize=256m
+# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
+
+# When configured, Gradle will run in incubating parallel mode.
+# This option should only be used with decoupled projects. More details, visit
+# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
+# org.gradle.parallel=true

BIN
gradle/wrapper/gradle-wrapper.jar


+ 6 - 0
gradle/wrapper/gradle-wrapper.properties

@@ -0,0 +1,6 @@
+#Mon Dec 28 10:00:20 PST 2015
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

+ 160 - 0
gradlew

@@ -0,0 +1,160 @@
+#!/usr/bin/env bash
+
+##############################################################################
+##
+##  Gradle start up script for UN*X
+##
+##############################################################################
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS=""
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn ( ) {
+    echo "$*"
+}
+
+die ( ) {
+    echo
+    echo "$*"
+    echo
+    exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+case "`uname`" in
+  CYGWIN* )
+    cygwin=true
+    ;;
+  Darwin* )
+    darwin=true
+    ;;
+  MINGW* )
+    msys=true
+    ;;
+esac
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+    ls=`ls -ld "$PRG"`
+    link=`expr "$ls" : '.*-> \(.*\)$'`
+    if expr "$link" : '/.*' > /dev/null; then
+        PRG="$link"
+    else
+        PRG=`dirname "$PRG"`"/$link"
+    fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+        # IBM's JDK on AIX uses strange locations for the executables
+        JAVACMD="$JAVA_HOME/jre/sh/java"
+    else
+        JAVACMD="$JAVA_HOME/bin/java"
+    fi
+    if [ ! -x "$JAVACMD" ] ; then
+        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+    fi
+else
+    JAVACMD="java"
+    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
+    MAX_FD_LIMIT=`ulimit -H -n`
+    if [ $? -eq 0 ] ; then
+        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+            MAX_FD="$MAX_FD_LIMIT"
+        fi
+        ulimit -n $MAX_FD
+        if [ $? -ne 0 ] ; then
+            warn "Could not set maximum file descriptor limit: $MAX_FD"
+        fi
+    else
+        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+    fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin ; then
+    APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+    JAVACMD=`cygpath --unix "$JAVACMD"`
+
+    # We build the pattern for arguments to be converted via cygpath
+    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+    SEP=""
+    for dir in $ROOTDIRSRAW ; do
+        ROOTDIRS="$ROOTDIRS$SEP$dir"
+        SEP="|"
+    done
+    OURCYGPATTERN="(^($ROOTDIRS))"
+    # Add a user-defined pattern to the cygpath arguments
+    if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+        OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+    fi
+    # Now convert the arguments - kludge to limit ourselves to /bin/sh
+    i=0
+    for arg in "$@" ; do
+        CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+        CHECK2=`echo "$arg"|egrep -c "^-"`                                 ### Determine if an option
+
+        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### Added a condition
+            eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+        else
+            eval `echo args$i`="\"$arg\""
+        fi
+        i=$((i+1))
+    done
+    case $i in
+        (0) set -- ;;
+        (1) set -- "$args0" ;;
+        (2) set -- "$args0" "$args1" ;;
+        (3) set -- "$args0" "$args1" "$args2" ;;
+        (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+        (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+        (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+        (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+        (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+        (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+    esac
+fi
+
+# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
+function splitJvmOpts() {
+    JVM_OPTS=("$@")
+}
+eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
+JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
+
+exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"

+ 90 - 0
gradlew.bat

@@ -0,0 +1,90 @@
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem  Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS=
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windowz variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+if "%@eval[2+2]" == "4" goto 4NT_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+goto execute
+
+:4NT_args
+@rem Get arguments from the 4NT Shell from JP Software
+set CMD_LINE_ARGS=%$
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if  not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega

+ 1 - 0
library/.gitignore

@@ -0,0 +1 @@
+/build

+ 25 - 0
library/build.gradle

@@ -0,0 +1,25 @@
+apply plugin: 'com.android.library'
+
+android {
+    compileSdkVersion 24
+    buildToolsVersion "23.0.3"
+
+    defaultConfig {
+        minSdkVersion 11
+        targetSdkVersion 24
+        versionCode 1
+        versionName "1.0"
+    }
+    buildTypes {
+        release {
+            minifyEnabled false
+            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+        }
+    }
+}
+
+dependencies {
+    compile fileTree(dir: 'libs', include: ['*.jar'])
+    testCompile 'junit:junit:4.12'
+    compile 'com.android.support:appcompat-v7:24.1.1'
+}

+ 17 - 0
library/proguard-rules.pro

@@ -0,0 +1,17 @@
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in /Users/sweetspot/Library/Android/sdk/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the proguardFiles
+# directive in build.gradle.
+#
+# For more details, see
+#   http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+#   public *;
+#}

+ 13 - 0
library/src/androidTest/java/top/zibin/luban/ApplicationTest.java

@@ -0,0 +1,13 @@
+package top.zibin.luban;
+
+import android.app.Application;
+import android.test.ApplicationTestCase;
+
+/**
+ * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
+ */
+public class ApplicationTest extends ApplicationTestCase<Application> {
+    public ApplicationTest() {
+        super(Application.class);
+    }
+}

+ 8 - 0
library/src/main/AndroidManifest.xml

@@ -0,0 +1,8 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="top.zibin.luban">
+
+    <application android:allowBackup="true" android:label="@string/app_name"
+        android:supportsRtl="true">
+
+    </application>
+
+</manifest>

+ 359 - 0
library/src/main/java/top/zibin/luban/Luban.java

@@ -0,0 +1,359 @@
+package top.zibin.luban;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Matrix;
+import android.media.ExifInterface;
+import android.support.annotation.NonNull;
+import android.util.Log;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import static top.zibin.luban.Preconditions.checkNotNull;
+
+public class Luban {
+
+    public static final int FIRST_GEAR = 1;
+    public static final int THIRD_GEAR = 3;
+
+    private static final String TAG = "Luban";
+    public static String DEFAULT_DISK_CACHE_DIR = "luban_disk_cache";
+
+    private static volatile Luban INSTANCE;
+
+    private final File mCacheDir;
+
+    private OnCompressListener compressListener;
+    private File mFile;
+    private int gear;
+
+    Luban(File cacheDir) {
+        mCacheDir = cacheDir;
+    }
+
+    /**
+     * Returns a directory with a default name in the private cache directory of the application to use to store
+     * retrieved media and thumbnails.
+     *
+     * @param context A context.
+     * @see #getPhotoCacheDir(android.content.Context, String)
+     */
+    public static File getPhotoCacheDir(Context context) {
+        return getPhotoCacheDir(context, Luban.DEFAULT_DISK_CACHE_DIR);
+    }
+
+    /**
+     * Returns a directory with the given name in the private cache directory of the application to use to store
+     * retrieved media and thumbnails.
+     *
+     * @param context   A context.
+     * @param cacheName The name of the subdirectory in which to store the cache.
+     * @see #getPhotoCacheDir(android.content.Context)
+     */
+    public static File getPhotoCacheDir(Context context, String cacheName) {
+        File cacheDir = context.getCacheDir();
+        if (cacheDir != null) {
+            File result = new File(cacheDir, cacheName);
+            if (!result.mkdirs() && (!result.exists() || !result.isDirectory())) {
+                // File wasn't able to create a directory, or the result exists but not a directory
+                return null;
+            }
+            return result;
+        }
+        if (Log.isLoggable(TAG, Log.ERROR)) {
+            Log.e(TAG, "default disk cache dir is null");
+        }
+        return null;
+    }
+
+    public static Luban get(Context context) {
+        if (INSTANCE == null) INSTANCE = new Luban(Luban.getPhotoCacheDir(context));
+        return INSTANCE;
+    }
+
+    public Luban launch() {
+        checkNotNull(mFile, "the image file cannot be null, please call .load() before this method!");
+
+        if (gear == Luban.FIRST_GEAR)
+            firstCompress(mFile);
+        else if (gear == Luban.THIRD_GEAR)
+            thirdCompress(mFile.getAbsolutePath());
+
+        return this;
+    }
+
+    public Luban load(File file) {
+        mFile = file;
+        return this;
+    }
+
+    public Luban setCompressListener(OnCompressListener listener) {
+        compressListener = listener;
+        return this;
+    }
+
+    public Luban putGear(int gear) {
+        this.gear = gear;
+        return this;
+    }
+
+    private void thirdCompress(@NonNull String filePath) {
+        String thumb = mCacheDir.getAbsolutePath() + "/" + System.currentTimeMillis();
+
+        double size;
+
+        int angle = getImageSpinAngle(filePath);
+        int width = getImageSize(filePath)[0];
+        int height = getImageSize(filePath)[1];
+        int thumbW = width % 2 == 1 ? width + 1 : width;
+        int thumbH = height % 2 == 1 ? height + 1 : height;
+
+        width = thumbW > thumbH ? thumbH : thumbW;
+        height = thumbW > thumbH ? thumbW : thumbH;
+
+        double scale = ((double) width / height);
+
+        if (scale <= 1 && scale > 0.5625) {
+            if (height < 1664) {
+                size = ((width * height) / (1664.0 * 1664.0)) * 150;
+                size = size < 50 ? 50 : size;
+            } else if (height >= 1664 && height < 4990) {
+                thumbW = width / 2;
+                thumbH = height / 2;
+                size = ((thumbW * thumbH) / (4990.0 * 4990.0)) * 300;
+                size = size < 50 ? 50 : size;
+            } else if (height >= 4990 && height < 10240) {
+                thumbW = width / 4;
+                thumbH = height / 4;
+                size = (thumbW * thumbH) / (10240.0 * 10240.0) * 300;
+                size = size < 100 ? 100 : size;
+            } else {
+                int multiple = height / 1280;
+                thumbW = width / multiple;
+                thumbH = height / multiple;
+                size = (long) (((thumbW * thumbH) / Math.pow(1280 * multiple, 2)) * 300);
+                size = size < 100 ? 100 : size;
+            }
+        } else if (scale <= 0.5625 && scale > 0.5) {
+            int multiple = height / 1280;
+            thumbW = width / multiple;
+            thumbH = height / multiple;
+            size = (long) ((thumbW * thumbH) / Math.pow(1280 * multiple, 2) * 200);
+            size = size < 100 ? 100 : size;
+        } else {
+            int multiple = height / 1280;
+            thumbW = (int) (width / (scale * multiple));
+            thumbH = (int) (height / (scale * multiple));
+            size = (long) ((thumbW * thumbH) / Math.pow(1280 * (scale * multiple), 2) * 500);
+            size = size < 150 ? 150 : size;
+        }
+
+        compress(filePath, thumb, thumbW, thumbH, angle, (long) size);
+    }
+
+    private void firstCompress(@NonNull File file) {
+        int minSize = 60;
+        int longSide = 720;
+        int shortSide = 1280;
+
+        String filePath = file.getAbsolutePath();
+        String thumbFilePath = mCacheDir.getAbsolutePath() + "/" + System.currentTimeMillis();
+        long size = 0;
+        long maxSize = file.length() / 5;
+
+        int angle = getImageSpinAngle(filePath);
+        int[] imgSize = getImageSize(filePath);
+        int width = 0, height = 0;
+        if (imgSize[0] <= imgSize[1]) {
+            double scale = (double) imgSize[0] / (double) imgSize[1];
+            if (scale <= 1.0 && scale > 0.5625) {
+                width = imgSize[0] > shortSide ? shortSide : imgSize[0];
+                height = width * imgSize[1] / imgSize[0];
+                size = minSize;
+            } else if (scale <= 0.5625) {
+                height = imgSize[1] > longSide ? longSide : imgSize[1];
+                width = height * imgSize[0] / imgSize[1];
+                size = maxSize;
+            }
+        } else {
+            double scale = (double) imgSize[1] / (double) imgSize[0];
+            if (scale <= 1.0 && scale > 0.5625) {
+                height = imgSize[1] > shortSide ? shortSide : imgSize[1];
+                width = height * imgSize[0] / imgSize[1];
+                size = minSize;
+            } else if (scale <= 0.5625) {
+                width = imgSize[0] > longSide ? longSide : imgSize[0];
+                height = width * imgSize[1] / imgSize[0];
+                size = maxSize;
+            }
+        }
+
+        compress(filePath, thumbFilePath, width, height, angle, size);
+    }
+
+    /**
+     * obtain the image's width and height
+     *
+     * @param imagePath the path of image
+     */
+    public int[] getImageSize(String imagePath) {
+        int[] res = new int[2];
+
+        BitmapFactory.Options options = new BitmapFactory.Options();
+        options.inJustDecodeBounds = true;
+        options.inSampleSize = 1;
+        BitmapFactory.decodeFile(imagePath, options);
+
+        res[0] = options.outWidth;
+        res[1] = options.outHeight;
+
+        return res;
+    }
+
+    /**
+     * obtain the thumbnail that specify the size
+     *
+     * @param imagePath the target image path
+     * @param width     the width of thumbnail
+     * @param height    the height of thumbnail
+     * @return {@link Bitmap}
+     */
+    private Bitmap compress(String imagePath, int width, int height) {
+        BitmapFactory.Options options = new BitmapFactory.Options();
+        options.inJustDecodeBounds = true;
+        BitmapFactory.decodeFile(imagePath, options);
+
+        int outH = options.outHeight;
+        int outW = options.outWidth;
+        int inSampleSize = 1;
+
+        if (outH > height || outW > width) {
+            int halfH = outH / 2;
+            int halfW = outW / 2;
+
+            while ((halfH / inSampleSize) > height && (halfW / inSampleSize) > width) {
+                inSampleSize *= 2;
+            }
+        }
+
+        options.inSampleSize = inSampleSize;
+
+        options.inJustDecodeBounds = false;
+
+        int heightRatio = (int) Math.ceil(options.outHeight / (float) height);
+        int widthRatio = (int) Math.ceil(options.outWidth / (float) width);
+
+        if (heightRatio > 1 || widthRatio > 1) {
+            if (heightRatio > widthRatio) {
+                options.inSampleSize = heightRatio;
+            } else {
+                options.inSampleSize = widthRatio;
+            }
+        }
+        options.inJustDecodeBounds = false;
+
+        return BitmapFactory.decodeFile(imagePath, options);
+    }
+
+    /**
+     * obtain the image rotation angle
+     *
+     * @param path path of target image
+     */
+    private int getImageSpinAngle(String path) {
+        int degree = 0;
+        try {
+            ExifInterface exifInterface = new ExifInterface(path);
+            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
+            switch (orientation) {
+                case ExifInterface.ORIENTATION_ROTATE_90:
+                    degree = 90;
+                    break;
+                case ExifInterface.ORIENTATION_ROTATE_180:
+                    degree = 180;
+                    break;
+                case ExifInterface.ORIENTATION_ROTATE_270:
+                    degree = 270;
+                    break;
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return degree;
+    }
+
+    /**
+     * 指定参数压缩图片
+     * create the thumbnail with the true rotate angle
+     *
+     * @param largeImagePath the big image path
+     * @param thumbFilePath  the thumbnail path
+     * @param width          width of thumbnail
+     * @param height         height of thumbnail
+     * @param angle          rotation angle of thumbnail
+     * @param size           the file size of image
+     */
+    private void compress(String largeImagePath, String thumbFilePath, int width, int height, int angle, long size) {
+        Bitmap thbBitmap = compress(largeImagePath, width, height);
+
+        thbBitmap = rotatingImage(angle, thbBitmap);
+
+        saveImage(thumbFilePath, thbBitmap, size);
+    }
+
+    /**
+     * 旋转图片
+     * rotate the image with specified angle
+     *
+     * @param angle  the angle will be rotating 旋转的角度
+     * @param bitmap target image               目标图片
+     */
+    private static Bitmap rotatingImage(int angle, Bitmap bitmap) {
+        //rotate image
+        Matrix matrix = new Matrix();
+        matrix.postRotate(angle);
+
+        //create a new image
+        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
+    }
+
+    /**
+     * 保存图片到指定路径
+     * Save image with specified size
+     *
+     * @param filePath the image file save path 储存路径
+     * @param bitmap   the image what be save   目标图片
+     * @param size     the file size of image   期望大小
+     */
+    private void saveImage(String filePath, Bitmap bitmap, long size) {
+        checkNotNull(bitmap, TAG + "bitmap cannot be null");
+
+        File result = new File(filePath.substring(0, filePath.lastIndexOf("/")));
+
+        if (!result.exists() && !result.mkdirs()) return;
+
+        ByteArrayOutputStream stream = new ByteArrayOutputStream();
+        int options = 100;
+        bitmap.compress(Bitmap.CompressFormat.JPEG, options, stream);
+
+        while (stream.toByteArray().length / 1024 > size) {
+            stream.reset();
+            options -= 5;
+            bitmap.compress(Bitmap.CompressFormat.JPEG, options, stream);
+        }
+        try {
+            FileOutputStream fos = new FileOutputStream(filePath);
+            fos.write(stream.toByteArray());
+            fos.flush();
+            fos.close();
+
+            if (compressListener != null) compressListener.onSuccess(new File(filePath));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 7 - 0
library/src/main/java/top/zibin/luban/OnCompressListener.java

@@ -0,0 +1,7 @@
+package top.zibin.luban;
+
+import java.io.File;
+
+public interface OnCompressListener {
+    void onSuccess(File file);
+}

+ 38 - 0
library/src/main/java/top/zibin/luban/Preconditions.java

@@ -0,0 +1,38 @@
+package top.zibin.luban;
+
+import android.support.annotation.Nullable;
+
+public final class Preconditions {
+
+    /**
+     * Ensures that an object reference passed as a parameter to the calling method is not null.
+     *
+     * @param reference an object reference
+     * @return the non-null reference that was validated
+     * @throws NullPointerException if {@code reference} is null
+     */
+    public static <T> T checkNotNull(T reference) {
+        if (reference == null) {
+            throw new NullPointerException();
+        }
+        return reference;
+    }
+
+    /**
+     * Ensures that an object reference passed as a parameter to the calling method is not null.
+     *
+     * @param reference    an object reference
+     * @param errorMessage the exception message to use if the check fails; will be converted to a
+     *                     string using {@link String#valueOf(Object)}
+     * @return the non-null reference that was validated
+     * @throws NullPointerException if {@code reference} is null
+     */
+    public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) {
+        if (reference == null) {
+            throw new NullPointerException(String.valueOf(errorMessage));
+        }
+        return reference;
+    }
+
+
+}

+ 3 - 0
library/src/main/res/values/strings.xml

@@ -0,0 +1,3 @@
+<resources>
+    <string name="app_name">Luban</string>
+</resources>

+ 15 - 0
library/src/test/java/top/zibin/luban/ExampleUnitTest.java

@@ -0,0 +1,15 @@
+package top.zibin.luban;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * To work on unit tests, switch the Test Artifact in the Build Variants view.
+ */
+public class ExampleUnitTest {
+    @Test
+    public void addition_isCorrect() throws Exception {
+        assertEquals(4, 2 + 2);
+    }
+}

+ 1 - 0
settings.gradle

@@ -0,0 +1 @@
+include ':example', ':library'