convert content:// uri to real path when needed

This commit is contained in:
tibbi 2016-10-09 18:54:36 +02:00
parent be5eefb9a6
commit 647a487fd5
4 changed files with 30 additions and 13 deletions

View file

@ -6,8 +6,10 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
@ -133,4 +135,19 @@ public class Utils {
View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE); 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();
}
}
}
} }

View file

@ -1,5 +1,6 @@
package com.simplemobiletools.gallery.fragments; package com.simplemobiletools.gallery.fragments;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -12,6 +13,7 @@ import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.models.Medium; import com.simplemobiletools.gallery.models.Medium;
public class PhotoFragment extends ViewPagerFragment implements View.OnClickListener { 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); final View view = inflater.inflate(R.layout.pager_photo_item, container, false);
mMedium = (Medium) getArguments().getSerializable(Constants.MEDIUM); mMedium = (Medium) getArguments().getSerializable(Constants.MEDIUM);
if (mMedium.getPath().startsWith("content://"))
mMedium.setPath(Utils.getRealPathFromURI(getContext(), Uri.parse(mMedium.getPath())));
if (mMedium == null) if (mMedium == null)
return view; return view;

View file

@ -6,11 +6,11 @@ import java.io.Serializable;
public class Medium implements Serializable, Comparable { public class Medium implements Serializable, Comparable {
private static final long serialVersionUID = -6543139465975455L; private static final long serialVersionUID = -6543139465975455L;
private final String mPath;
private final boolean mIsVideo; private final boolean mIsVideo;
private final long mTimestamp; private final long mTimestamp;
private final long mSize; private final long mSize;
public static int mSorting; public static int mSorting;
private String mPath;
public Medium(String path, boolean isVideo, long timestamp, long size) { public Medium(String path, boolean isVideo, long timestamp, long size) {
mPath = path; mPath = path;
@ -19,6 +19,10 @@ public class Medium implements Serializable, Comparable {
mSize = size; mSize = size;
} }
public void setPath(String path) {
mPath = path;
}
public String getPath() { public String getPath() {
return mPath; return mPath;
} }
@ -66,6 +70,7 @@ public class Medium implements Serializable, Comparable {
return "Medium {" + return "Medium {" +
"isVideo=" + getIsVideo() + "isVideo=" + getIsVideo() +
", timestamp=" + getTimestamp() + ", timestamp=" + getTimestamp() +
", size=" + getSize() +
", path=" + getPath() + "}"; ", path=" + getPath() + "}";
} }
} }

View file

@ -5,11 +5,11 @@ import android.graphics.Bitmap
import android.media.MediaScannerConnection import android.media.MediaScannerConnection
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore
import android.util.Log import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.extensions.toast import com.simplemobiletools.gallery.extensions.toast
import com.theartofdev.edmodo.cropper.CropImageView import com.theartofdev.edmodo.cropper.CropImageView
import kotlinx.android.synthetic.main.activity_edit.* import kotlinx.android.synthetic.main.activity_edit.*
@ -69,7 +69,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
if (uri.scheme == "file") { if (uri.scheme == "file") {
saveBitmapToFile(result.bitmap, uri.path) saveBitmapToFile(result.bitmap, uri.path)
} else if (uri.scheme == "content") { } else if (uri.scheme == "content") {
val newPath = convertMediaUriToPath(uri) ?: "" val newPath = Utils.getRealPathFromURI(applicationContext, uri) ?: ""
if (!newPath.isEmpty()) { if (!newPath.isEmpty()) {
saveBitmapToFile(result.bitmap, newPath) saveBitmapToFile(result.bitmap, newPath)
} else { } else {
@ -123,14 +123,4 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
else -> Bitmap.CompressFormat.JPEG 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
}
} }