From 647a487fd5e616c0e61c54099d1e51b5f002ec8f Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 9 Oct 2016 18:54:36 +0200 Subject: [PATCH] convert content:// uri to real path when needed --- .../com/simplemobiletools/gallery/Utils.java | 17 +++++++++++++++++ .../gallery/fragments/PhotoFragment.java | 5 +++++ .../gallery/models/Medium.java | 7 ++++++- .../gallery/activities/EditActivity.kt | 14 ++------------ 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/Utils.java b/app/src/main/java/com/simplemobiletools/gallery/Utils.java index bd3cbcbf9..e3bb828e8 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Utils.java +++ b/app/src/main/java/com/simplemobiletools/gallery/Utils.java @@ -6,8 +6,10 @@ import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.Resources; +import android.database.Cursor; import android.net.Uri; import android.os.Build; +import android.provider.MediaStore; import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.util.DisplayMetrics; @@ -133,4 +135,19 @@ public class Utils { View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE); } + + public static String getRealPathFromURI(Context context, Uri uri) { + Cursor cursor = null; + try { + String[] projection = {MediaStore.Images.Media.DATA}; + cursor = context.getContentResolver().query(uri, projection, null, null, null); + int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); + cursor.moveToFirst(); + return cursor.getString(index); + } finally { + if (cursor != null) { + cursor.close(); + } + } + } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/fragments/PhotoFragment.java b/app/src/main/java/com/simplemobiletools/gallery/fragments/PhotoFragment.java index 7b7d48078..b05f8b537 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/fragments/PhotoFragment.java +++ b/app/src/main/java/com/simplemobiletools/gallery/fragments/PhotoFragment.java @@ -1,5 +1,6 @@ package com.simplemobiletools.gallery.fragments; +import android.net.Uri; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -12,6 +13,7 @@ import com.davemorrissey.labs.subscaleview.ImageSource; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.R; +import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.models.Medium; public class PhotoFragment extends ViewPagerFragment implements View.OnClickListener { @@ -23,6 +25,9 @@ public class PhotoFragment extends ViewPagerFragment implements View.OnClickList final View view = inflater.inflate(R.layout.pager_photo_item, container, false); mMedium = (Medium) getArguments().getSerializable(Constants.MEDIUM); + if (mMedium.getPath().startsWith("content://")) + mMedium.setPath(Utils.getRealPathFromURI(getContext(), Uri.parse(mMedium.getPath()))); + if (mMedium == null) return view; diff --git a/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java b/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java index 73205fe7c..e9b4703db 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java +++ b/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java @@ -6,11 +6,11 @@ import java.io.Serializable; public class Medium implements Serializable, Comparable { private static final long serialVersionUID = -6543139465975455L; - private final String mPath; private final boolean mIsVideo; private final long mTimestamp; private final long mSize; public static int mSorting; + private String mPath; public Medium(String path, boolean isVideo, long timestamp, long size) { mPath = path; @@ -19,6 +19,10 @@ public class Medium implements Serializable, Comparable { mSize = size; } + public void setPath(String path) { + mPath = path; + } + public String getPath() { return mPath; } @@ -66,6 +70,7 @@ public class Medium implements Serializable, Comparable { return "Medium {" + "isVideo=" + getIsVideo() + ", timestamp=" + getTimestamp() + + ", size=" + getSize() + ", path=" + getPath() + "}"; } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt index b3614e174..711375db6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -5,11 +5,11 @@ import android.graphics.Bitmap import android.media.MediaScannerConnection import android.net.Uri import android.os.Bundle -import android.provider.MediaStore import android.util.Log import android.view.Menu import android.view.MenuItem import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.Utils import com.simplemobiletools.gallery.extensions.toast import com.theartofdev.edmodo.cropper.CropImageView import kotlinx.android.synthetic.main.activity_edit.* @@ -69,7 +69,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener if (uri.scheme == "file") { saveBitmapToFile(result.bitmap, uri.path) } else if (uri.scheme == "content") { - val newPath = convertMediaUriToPath(uri) ?: "" + val newPath = Utils.getRealPathFromURI(applicationContext, uri) ?: "" if (!newPath.isEmpty()) { saveBitmapToFile(result.bitmap, newPath) } else { @@ -123,14 +123,4 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener else -> Bitmap.CompressFormat.JPEG } } - - private fun convertMediaUriToPath(uri: Uri): String? { - val proj = arrayOf(MediaStore.Images.Media.DATA) - val cursor = contentResolver.query(uri, proj, null, null, null) - val index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA) - cursor.moveToFirst() - val path = cursor.getString(index) - cursor.close() - return path - } }