我有一个 ImageView。当您单击 ImageView 时,它会打开图库,然后您选择一张图像并将其显示在 ImageView 上。我有一个条件,当我关闭我的应用程序然后打开它时,图像将保留在那里。 因此,为此,我将图像 Uri 存储在 sharedprefrence 上。 在打开应用程序时,我正在检索相同的 Uri 并尝试在 imageView 上显示图像。
但是在某些手机中 - 图像看起来很完美,例如 Mi(Lollipop)、Samsung(KitKat) 但它不会出现在摩托罗拉(棉花糖)、一加一(棉花糖)等手机中。 知道为什么会这样吗? 这是我的代码
用于获取我正在使用的图像
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)
但是如果我注意到,不同 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 上显示图像,而在其他情况下则不是
最佳答案
试试这个
我开发并测试
并致力于
- 联想K3 Note(棉花糖)
- 摩托罗拉( Lollipop )
- 三星(奇巧)
在你的 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" />
注意:
假设
您为Marshmallow
添加了take permission
代码
结果 Uri
lenovo 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
关于android - 图像 uri 不在某些 Android 设备上的 ImageView 上显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37698471/