From 76c2312b6d40a26a227b3b33c59f5adcb608260e Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 5 Nov 2016 23:50:51 +0100 Subject: [PATCH] convert photoProcessor to kotlin --- .../camera/PhotoProcessor.java | 75 ------------------- .../camera/PhotoProcessor.kt | 69 +++++++++++++++++ 2 files changed, 69 insertions(+), 75 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.java create mode 100644 app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.kt diff --git a/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.java b/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.java deleted file mode 100644 index 37e8c5f0..00000000 --- a/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.simplemobiletools.camera; - -import android.net.Uri; -import android.os.AsyncTask; -import android.util.Log; - -import com.simplemobiletools.camera.activities.MainActivity; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.lang.ref.WeakReference; - -public class PhotoProcessor extends AsyncTask { - private static final String TAG = PhotoProcessor.class.getSimpleName(); - private static WeakReference mActivity; - private static Uri mUri; - - public PhotoProcessor(MainActivity activity, Uri uri) { - mActivity = new WeakReference<>(activity); - mUri = uri; - } - - @Override - protected String doInBackground(byte[]... params) { - FileOutputStream fos = null; - String path; - try { - if (mUri != null) { - path = mUri.getPath(); - } else { - path = Utils.getOutputMediaFile(mActivity.get(), true); - } - - if (path.isEmpty()) { - return ""; - } - - final File photoFile = new File(path); - final byte[] data = params[0]; - fos = new FileOutputStream(photoFile); - fos.write(data); - fos.close(); - return photoFile.getAbsolutePath(); - } catch (FileNotFoundException e) { - Log.e(TAG, "PhotoProcessor file not found: " + e.getMessage()); - } catch (IOException e) { - Log.e(TAG, "PhotoProcessor ioexception " + e.getMessage()); - } finally { - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - Log.e(TAG, "PhotoProcessor close ioexception " + e.getMessage()); - } - } - } - - return ""; - } - - @Override - protected void onPostExecute(String path) { - super.onPostExecute(path); - final MediaSavedListener listener = mActivity.get(); - if (listener != null) { - listener.mediaSaved(path); - } - } - - public interface MediaSavedListener { - void mediaSaved(String path); - } -} diff --git a/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.kt b/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.kt new file mode 100644 index 00000000..4c217675 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.kt @@ -0,0 +1,69 @@ +package com.simplemobiletools.camera + +import android.net.Uri +import android.os.AsyncTask +import android.util.Log + +import com.simplemobiletools.camera.activities.MainActivity + +import java.io.File +import java.io.FileNotFoundException +import java.io.FileOutputStream +import java.io.IOException +import java.lang.ref.WeakReference + +class PhotoProcessor(activity: MainActivity, val uri: Uri?) : AsyncTask() { + companion object { + private val TAG = PhotoProcessor::class.java.simpleName + private var mActivity: WeakReference? = null + } + + init { + mActivity = WeakReference(activity) + } + + override fun doInBackground(vararg params: ByteArray): String { + var fos: FileOutputStream? = null + val path: String + try { + if (uri != null) { + path = uri.path + } else { + path = Utils.getOutputMediaFile(mActivity?.get(), true) + } + + if (path.isEmpty()) { + return "" + } + + val photoFile = File(path) + val data = params[0] + fos = FileOutputStream(photoFile) + fos.write(data) + fos.close() + return photoFile.absolutePath + } catch (e: FileNotFoundException) { + Log.e(TAG, "PhotoProcessor file not found: " + e.message) + } catch (e: IOException) { + Log.e(TAG, "PhotoProcessor ioexception " + e.message) + } finally { + try { + fos?.close() + } catch (e: IOException) { + Log.e(TAG, "PhotoProcessor close ioexception " + e.message) + } + } + + return "" + } + + override fun onPostExecute(path: String) { + super.onPostExecute(path) + val listener = mActivity?.get() + listener?.mediaSaved(path) + } + + interface MediaSavedListener { + fun mediaSaved(path: String) + } +}