diff --git a/app/src/main/java/org/fedorahosted/freeotp/edit/EditActivity.java b/app/src/main/java/org/fedorahosted/freeotp/edit/EditActivity.java index 575d1ad..3305a54 100644 --- a/app/src/main/java/org/fedorahosted/freeotp/edit/EditActivity.java +++ b/app/src/main/java/org/fedorahosted/freeotp/edit/EditActivity.java @@ -24,7 +24,10 @@ import org.fedorahosted.freeotp.R; import org.fedorahosted.freeotp.Token; import org.fedorahosted.freeotp.TokenPersistence; +import android.content.Context; import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.text.Editable; @@ -36,6 +39,13 @@ import android.widget.ImageButton; import com.squareup.picasso.Picasso; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.UUID; + public class EditActivity extends BaseActivity implements TextWatcher, View.OnClickListener { private EditText mIssuer; private EditText mLabel; @@ -113,7 +123,41 @@ public class EditActivity extends BaseActivity implements TextWatcher, View.OnCl super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) - showImage(data.getData()); + showImage(copyImage(getApplicationContext(), data.getData())); + } + + public static Uri copyImage(Context context, Uri uri) { + InputStream is = null; + FileOutputStream bytes = null; + String filename = null; + if (uri.getAuthority() != null) { + try { + is = context.getContentResolver().openInputStream(uri); + Bitmap inImage = BitmapFactory.decodeStream(is); + filename = UUID.randomUUID().toString() + ".jpg"; + bytes = context.openFileOutput(filename, Context.MODE_PRIVATE); + inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); + bytes.flush(); + File file = new File(context.getFilesDir().getAbsolutePath() + '/' + filename); + return Uri.fromFile(file); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + bytes.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return null; } @Override