convert photoProcessor to kotlin

This commit is contained in:
tibbi 2016-11-05 23:50:51 +01:00
parent 85416aa0db
commit 76c2312b6d
2 changed files with 69 additions and 75 deletions

View File

@ -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<byte[], Void, String> {
private static final String TAG = PhotoProcessor.class.getSimpleName();
private static WeakReference<MainActivity> 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);
}
}

View File

@ -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<ByteArray, Void, String>() {
companion object {
private val TAG = PhotoProcessor::class.java.simpleName
private var mActivity: WeakReference<MainActivity>? = 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)
}
}