diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 34dc27c..ce889bd 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -3,31 +3,115 @@ - - - - - - - - - - + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
diff --git a/README.md b/README.md index 4865d85..413157e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ -# compressor -image compressor library for android (testing) +# Image Compressor Library [ ANDROID ] +Compressor is an android image compression library. Compressor will allow you to compress large photos into smaller sized photos. + +### CODE + +``` + // Compress image using RxJava in background thread + Compressor(this@MainActivity) + .compressToFileAsFlowable(actualImage!!) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ file -> + compressedImage = file + setCompressedImage() + }, { throwable -> + throwable.printStackTrace() + }) +``` diff --git a/app/build.gradle b/app/build.gradle index 2f76242..207ef74 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,7 +12,7 @@ android { targetSdkVersion 28 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { @@ -25,12 +25,12 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'com.android.support:appcompat-v7:28.0.0' - implementation 'com.android.support.constraint:constraint-layout:1.1.3' + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 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' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' implementation project(path: ':imagecompressor') - implementation 'io.reactivex.rxjava2:rxandroid:2.1.0' + implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' implementation "org.jetbrains.anko:anko:0.10.8" } diff --git a/app/src/androidTest/java/com/qkopy/compressor/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/qkopy/compressor/ExampleInstrumentedTest.kt deleted file mode 100644 index 874df5a..0000000 --- a/app/src/androidTest/java/com/qkopy/compressor/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,22 +0,0 @@ -package com.qkopy.compressor - -import android.support.test.InstrumentationRegistry -import android.support.test.runner.AndroidJUnit4 -import org.junit.Assert.assertEquals -import org.junit.Test -import org.junit.runner.RunWith - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getTargetContext() - assertEquals("com.qkopy.compressor", appContext.packageName) - } -} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e284842..9fcdc53 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,10 +1,10 @@ + xmlns:tools="http://schemas.android.com/tools" package="com.qkopy.compressor" + > - { + var name = fileName + var extension: String? = "" + val i = fileName!!.lastIndexOf(".") + if (i != -1) { + name = fileName.substring(0, i) + extension = fileName.substring(i) + } + return arrayOf(name, extension) + } + + private fun getFileName( + context: Context, + uri: Uri + ): String? { + var result: String? = null + if (uri.scheme == "content") { + val cursor = + context.contentResolver.query(uri, null, null, null, null) + try { + if (cursor != null && cursor.moveToFirst()) { + result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)) + } + } catch (e: Exception) { + e.printStackTrace() + } finally { + cursor?.close() + } + } + if (result == null) { + result = uri.path + assert(result != null) + val cut = result.lastIndexOf(File.separator) + if (cut != -1) { + result = result.substring(cut + 1) + } + } + return result + } + + private fun rename(file: File, newName: String?): File { + val newFile = File(file.parent, newName) + if (newFile != file) { + if (newFile.exists() && newFile.delete()) { + Log.d("FileUtil", "Delete old $newName file") + } + if (file.renameTo(newFile)) { + Log.d("FileUtil", "Rename file to $newName") + } + } + return newFile + } + + @Throws(IOException::class) + private fun copy(input: InputStream, output: OutputStream?): Long { + var count: Long = 0 + var n: Int + val buffer = + ByteArray(DEFAULT_BUFFER_SIZE) + while (EOF != input.read(buffer).also { n = it }) { + output!!.write(buffer, 0, n) + count += n.toLong() + } + return count + } +} \ No newline at end of file diff --git a/app/src/main/java/com/qkopy/compressor/MainActivity.kt b/app/src/main/java/com/qkopy/compressor/MainActivity.kt index 124a2df..995befc 100644 --- a/app/src/main/java/com/qkopy/compressor/MainActivity.kt +++ b/app/src/main/java/com/qkopy/compressor/MainActivity.kt @@ -1,10 +1,15 @@ package com.qkopy.compressor +import android.annotation.SuppressLint import android.app.Activity import android.content.Intent +import android.graphics.Bitmap import android.graphics.BitmapFactory +import android.media.ExifInterface +import android.os.Build import android.os.Bundle -import android.support.v7.app.AppCompatActivity +import android.provider.MediaStore +import androidx.appcompat.app.AppCompatActivity import android.util.Log import android.view.View import com.qkopy.imagecompressor.Compressor @@ -23,20 +28,29 @@ class MainActivity : AppCompatActivity() { private val PICK_IMAGE_REQUEST = 100 private var actualImage: File? = null private var compressedImage: File? = null + private var compressedBitmap:Bitmap? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) clearImage() + } fun chooseImage(view: View) { - val intent = Intent(Intent.ACTION_GET_CONTENT) + val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + Intent(Intent.ACTION_OPEN_DOCUMENT) + } else { + Intent(Intent.ACTION_GET_CONTENT) + } + intent.addCategory(Intent.CATEGORY_OPENABLE) intent.type = "image/*" startActivityForResult(intent, PICK_IMAGE_REQUEST) + } + @SuppressLint("CheckResult") fun compressImage(view: View) { if (actualImage == null) { toast("Please choose an image!") @@ -51,12 +65,18 @@ class MainActivity : AppCompatActivity() { // Compress image using RxJava in background thread Compressor(this@MainActivity) + + //.setDestinationDirectoryPath(obbDir.path+File.separator+"image") + //.compressToBitmapAsFlowable(actualImage!!) .compressToFileAsFlowable(actualImage!!) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ file -> + compressedImage = file setCompressedImage() +// compressedBitmap = file +// setCompressedImageBitmap() }, { throwable -> throwable.printStackTrace() toast("$throwable.message") @@ -77,10 +97,20 @@ class MainActivity : AppCompatActivity() { private fun setCompressedImage() { compressed_image.setImageBitmap(BitmapFactory.decodeFile(compressedImage?.absolutePath)) compressed_size.text = String.format("Size : %s", getReadableFileSize(compressedImage?.length())) + val exif = ExifInterface(compressedImage?.absolutePath) + compressed_meta.text = "${exif.getAttribute(ExifInterface.TAG_DATETIME)}\n${exif.getAttribute(ExifInterface.TAG_SOFTWARE)}\n" + + "${exif.getAttribute(ExifInterface.TAG_Y_RESOLUTION)}" //Toast.makeText(this, "Compressed image save in " + compressedImage?.path, Toast.LENGTH_LONG).show() Log.d("Compressor", "Compressed image save in " + compressedImage?.path) } + private fun setCompressedImageBitmap() { + compressed_image.setImageBitmap(compressedBitmap) + compressed_size.text = String.format("Size : %s", getReadableFileSize(compressedBitmap?.byteCount?.toLong())) + //Toast.makeText(this, "Compressed image save in " + compressedImage?.path, Toast.LENGTH_LONG).show() + Log.d("Compressor", "Compressed image save in " + compressedBitmap?.config?.name) + } + private fun clearImage() { compressed_image.setImageDrawable(null) compressed_size.text = getString(R.string.size) @@ -98,6 +128,11 @@ class MainActivity : AppCompatActivity() { if (actualImage!!.absolutePath != null) { actual_image.setImageBitmap(BitmapFactory.decodeFile(actualImage!!.absolutePath)) actual_size.text = String.format("Size : %s", getReadableFileSize(actualImage!!.length())) + val exif = ExifInterface(actualImage?.absolutePath) + actual_meta.text = "${exif.getAttribute(ExifInterface.TAG_DATETIME)}\n${exif.getAttribute(ExifInterface.TAG_SOFTWARE)}\n" + + "${exif.getAttribute(ExifInterface.TAG_Y_RESOLUTION)}" + exif.toString() + clearImage() } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d6c7e04..d244e99 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -40,6 +40,7 @@ android:layout_weight="1" android:adjustViewBounds="true"/> + + + + + + + + +