[英]Image uri doesn't display images on ImageView on some android device
我有一个 ImageView。 当您单击 ImageView 时,它会打开图库,您会选择一张图像并将其显示在 ImageView 上。 我有一个条件,当我关闭我的应用程序然后打开它时,图像将保留在那里。 为此,我将图像 Uri 存储在 sharedprefence 上。 在打开应用程序时,我正在检索相同的 Uri 并尝试在 imageView 上显示图像。
然而,在某些手机中 - 图像看起来很完美,如 Mi(Lollipop)、三星(KitKat),但它不会出现在像摩托罗拉(Marshmallow)、一加一(Marshmallow)这样的手机中。 知道为什么会这样吗? 这是我的代码
用于获取我正在使用的图像
Intent intent=new Intent();intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), Constants.SELECT_PICTURE);
而 OnActivityResult() 代码是
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==Constants.SELECT_PICTURE && resultCode==RESULT_OK && null!=data) {
Uri uri=data.getData();
Picasso.with(UsersProfileActivity.this)
.load(uri)
.centerCrop()
.resize(200,200)
.into(img_photo);
// This profile image i am storing into a sharedpreference
profile_image=uri.toString();
}
在从 sharedprefrence 检索时,我使用Uri.parse(profile_image)
将 String 转换为 uri
但是,如果我注意到,不同 android 手机的 uri 返回如下
Mi(Lollipop)- Uri=content://media/external/images/media/12415
samsung(KitKat)-Uri=content://media/external/images/media/786
Motorola(Marshmallow)- Uri=content://com.android.providers.media.documents/document/image%3A30731
One Plus One (Marshmallow)- Uri=content://com.android.providers.media.documents/document/image%3A475
因此,当 uri 内容是 -content://media/external/images/media/ 在 ImageView Perfect 上显示图像时,在其他情况下则不是
试试这个
我开发、 tested
和工作
在您的MainActivity.java
添加
profilePicture = (ImageView)findViewById(R.id.imgProfilePicture);
sharedPreferences = getSharedPreferences(getString(R.string.app_name)+"_ProfileDetails",MODE_PRIVATE);
// get uri from shared preference
String profilePicUri = sharedPreferences.getString("profilePicUrl","");
// if uri not found
if (profilePicUri.equalsIgnoreCase("")){
// code for open gallery to select image
Intent intent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
}else{
intent = new Intent(Intent.ACTION_GET_CONTENT);
}
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE_CONSTANT);
}else{
try{
final int takeFlags = (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
getContentResolver().takePersistableUriPermission(Uri.parse(profilePicUri), takeFlags);
// convert uri to bitmap
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(profilePicUri));
// set bitmap to imageview
profilePicture.setImageBitmap(bitmap);
}
catch (Exception e){
//handle exception
e.printStackTrace();
}
}
现在处理onActivityResult
。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PICTURE_CONSTANT && resultCode == RESULT_OK && null != data) {
Uri uri = data.getData();
// This profile image i am storing into a sharedpreference
SharedPreferences.Editor editor = sharedPreferences.edit();
// save uri to shared preference
editor.putString("profilePicUrl",uri.toString());
editor.commit();
profilePicture.setImageURI(uri);
}
}
在您的Manifest
文件中添加permission
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
注意:
Assuming
您添加了take permission
Marshmallow
take permission
代码
结果Uri
联想 K3 注: content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera%2FIMG_20160606_212815.jpg
三星: content://com.android.providers.media.documents/document/image%3A2086
摩托罗拉: content://com.android.providers.media.documents/document/image%3A15828
我认为图像文件的 uri 有问题,我正在使用一个类来获取确切的 URI,并且它已经过测试并适用于所有版本。
package fandooo.com.fandooo.util;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
public class ImageFilePath {
/**
* Method for return file path of Gallery image
*
* @param context
* @param uri
* @return path of the selected image file from gallery
*/
public static String getPath(final Context context, final Uri uri) {
// check here to KITKAT or new version
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] { split[1] };
return getDataColumn(context, contentUri, selection,
selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context
* The context.
* @param uri
* The Uri to query.
* @param selection
* (Optional) Filter used in the query.
* @param selectionArgs
* (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri,
String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection,
selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri
.getAuthority());
}
/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri
.getAuthority());
}
/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri
.getAuthority());
}
/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri
.getAuthority());
}
}
使用 getPath 查找文件的确切路径。 这是处理您定义的问题的最简单方法。 如果您需要进一步的帮助,请尝试告诉我:)
引自4.4 API 概述:
'在以前版本的 Android 上,如果您希望您的应用程序从另一个应用程序检索特定类型的文件,则它必须使用ACTION_GET_CONTENT操作调用一个意图。 此操作仍然是请求要导入应用程序的文件的适当方式。 但是,Android 4.4 引入了ACTION_OPEN_DOCUMENT操作,它允许用户选择特定类型的文件并授予您的应用对该文件的长期读取访问权限(可能具有写入访问权限),而无需将该文件导入您的应用。'(强调添加)
要允许您的应用检索先前选择的图像,只需将操作从ACTION_GET_CONTENT更改为ACTION_OPEN_DOCUMENT (解决方案已确认在 Nexus 7 2013 上工作)。
Intent intent = new Intent();
intent.setType("image/*");
String action = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_GET_CONTENT;
intent.setAction(action);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), Constants.SELECT_PICTURE);
在此方法中传递您的 URI 它将返回一个字符串,然后将其转换为位图
String path = getPath(getApplicationContext(), uri);
Bitmap bitmap = BitmapFactory.decodeFile(path);
imageView.setImageBitmap(bitmap);
public static String getPath(final Context context, final Uri uri) {
// DocumentProvider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {// ExternalStorageProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
String storageDefinition;
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else {
if (Environment.isExternalStorageRemovable()) {
storageDefinition = "EXTERNAL_STORAGE";
} else {
storageDefinition = "SECONDARY_STORAGE";
}
return System.getenv(storageDefinition) + "/" + split[1];
}
} else if (isDownloadsDocument(uri)) {// DownloadsProvider
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri)) {// MediaProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore (and general)
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
return uri.getPath();
}
return null;
}
对于 Android 6+,您需要在运行时向用户请求读取外部存储的权限
https://developer.android.com/training/permissions/requesting.html#perm-check
希望这会有所帮助。
也许您可以通过先将 URI 转换为真实路径来处理它。
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
你的 onActivityResult 应该是这样的
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==Constants.SELECT_PICTURE && resultCode==RESULT_OK && null!=data)
{
Uri uri=data.getData();
String imagePath = getRealPathFromURI(getApplicationContext(), uril);
Picasso.with(UsersProfileActivity.this).load(imagePath).centerCrop().resize(200,200).into(img_photo);
profile_image = imagePath;
}
我过去曾设法做到这一点。 当 onSaveInstanceState(Bundle) 被调用时,我没有将它存储在 SharePreferences 中,而是将它保存在 Bundle 中:bundle.putParcelable(SOME_KEY, uri)。 您可以这样做,因为 Uri 实现了 Parcelable。 之后,在 onCreate() 传入的 Bundle 中检查这个 uri。 代码:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
loadImage((Uri)saveInstanceState.getParcelable(SOME_KEY))
}
}
public void onSaveInstanceState(Bundle bundle){
bundle.putParcelable(SOME_KEY, uri)
}
P/S:我想在 SharePreference 中存储 uri 的问题是编码/解码 Uri。
您可以轻松地将 URI 转换为位图:
ParcelFileDescriptor parcelFileDescriptor =
context.getContentResolver().openFileDescriptor(selectedFileUri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
然后在Imageview中设置: imgview.setImageBitmap(image);
希望这会帮助你。
可能是毕加索版本控制问题。 他们修复了在版本 2.5.0 (2.4.0...?) 中加载大图像的问题,该版本尚无法通过存储库获得。
您需要手动添加毕加索的夜间快照,这可能会解决您的问题。 它对我有用。
我之前也遇到过类似的问题,并找到了一个有用的帮助类,它可以为您提供文件的确切位置。 这是链接。
您可以使用 FileUtils.getPath(context, uri) 或 FileUtils.getFile(context, uri) 等方法来获取文件。 然后像这样使用它:
Picasso.with(UsersProfileActivity.this)
.load(Uri.fromFile(file))
.centerCrop()
.resize(200,200)
.into(img_photo);
希望这会有所帮助。
只是不是 100% 确定我是否收到问题。 如果某些图像无法在 imageView 中显示(甚至在关闭并重新启动应用程序之前),那么问题在于 Picasso 能够处理来自某些提供商的内容。 我不明白是这种情况,但如果是这样,那么您可能需要考虑为该库提交错误/RFE。 另一方面,如果 Picasso 正确显示了给定 Uri 的图像,但不是在 Uri_org -> String -> Uri_regen 转换之后,则意味着 Uri_org 与 Uri_regen 不同。 您可以(暂时)添加几行代码以将字符串转换回 Uri 并在其后放置一个断点并比较两个 Uri。 这应该会给你一个关于事情是如何出错的线索。
profile_image=uri.toString();
//add the next few lines for debugging purposes only; remove them later
Uri uri2 = Uri.parse(profile_image)
if (!uri.equals(uri2)
//add a breakpoint on the next line
Log.d("Uri mismatch), "Ouch");
//remove the lines after you are done with debugging
如果您没有遇到断点,则将断点移回“if”语句并直观地检查两个 Uri(几乎肯定会相同,否则您会遇到断点,但这将有助于检查调试代码本身)。 如果 Uri 是相同的,那么不知何故字符串首选项由于某种原因没有被正确保留。 在这种情况下,记下原始 Uri 并在将 String 转换回 Uri 的行上添加一个断点:
Uri uri = Uri.parse(profile_image) //profile_image is retrieved from sharedpreferences
如果之前的实验有效,除非 profile_image 不同,否则它应该在这里再次有效。 无论哪种方式,您都应该获得有关问题所在的指示。
祝你好运!
一个简单的修复方法是在文件路径的开头添加“file://”。 例如:将{{uri: "/path/to/file"}}
更改为{{uri: "file:///path/to/file"}}
适用于任何平台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.