问题描述
我有一个 ImageView.当您单击 ImageView 时,它会打开图库,然后您会选择一张图像并将其显示在 ImageView 上.我有一个条件,当我关闭我的应用程序然后打开它时,图像将保留在那里.因此,为此,我将图像 Uri 存储在 sharedprefrence 上.在打开应用程序时,我正在检索相同的 Uri 并尝试在 imageView 上显示图像.
I have an ImageView. When you click on a ImageView, it will open gallery and you pick up a image and show it on ImageView. I have a condition that when I close my app and then I open it, the image will retain there . So for that, I am storing image Uri on sharedprefrence. And on opening the application i am retrieving the same Uri and tries to display the image on imageView.
但是在某些手机中 - 图像看起来很完美,例如 Mi(Lollipop)、Samsung(KitKat)但它不会出现在摩托罗拉(Marshmallow)、一加一(Marshmallow)等手机中.知道为什么会这样吗?这是我的代码
However in some phones - image appears perfect like Mi(Lollipop), Samsung(KitKat) but it doesn't appear in phones like - Motorola(Marshmallow) ,One Plus One (Marshmallow). Any idea why it's happening ? Here is my code
为了拾取我正在使用的图像
For picking up an image i am using
Intent intent=new Intent();intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), Constants.SELECT_PICTURE);
OnActivityResult() 代码是
And on OnActivityResult() code is
@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)
And while retrieving from sharedprefrence I convert String to uri by using Uri.parse(profile_image)
但是,如果我注意到,不同安卓手机的 uri 返回如下
However if I notice, uri returns for different android phone is as follows
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 上显示图像,而在其他情况下则不是
Hence when uri content is -content://media/external/images/media/ is display image on ImageView Perfect and in other cases it is not
推荐答案
试试这个
我开发并测试过
并正在努力
I developed and tested
and working on
- Lenovo K3 Note(棉花糖)
- 摩托罗拉(棒棒糖)
- 三星(奇巧)
在你的 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
In your Manifest
file add 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
结果 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
这篇关于图像 uri 在某些 android 设备上的 ImageView 上不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!