abh*_*hek 14 android uri image imageview picasso
我有一个ImageView.当您单击ImageView时,它将打开图库,您将获取图像并在ImageView上显示它.我有一个条件,当我关闭我的应用程序,然后我打开它,图像将保留在那里.所以,为此,我将图像Uri存储在sharedprefrence上.在打开应用程序时,我正在检索相同的Uri并尝试在imageView上显示图像.
然而在一些手机中 - 图像看起来像Mi(Lollipop),三星(KitKat)一样完美,但它并没有出现在像摩托罗拉(Marshmallow),One Plus One(Marshmallow)这样的手机中.知道为什么会这样吗?这是我的代码
拿起我正在使用的图像
Intent intent=new Intent();intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), Constants.SELECT_PICTURE);
Run Code Online (Sandbox Code Playgroud)
在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();
}
Run Code Online (Sandbox Code Playgroud)
在从sharedprefrence检索时,我通过使用将String转换为uri Uri.parse(profile_image)
但是如果我注意到,uri为不同的Android手机返回如下
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
Run Code Online (Sandbox Code Playgroud)
因此,当uri内容是-content:// media/external/images/media /是在ImageView Perfect上显示图像时,在其他情况下它不是
Pra*_*are 14
试试这个
我开发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();
}
}
Run Code Online (Sandbox Code Playgroud)
现在处理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);
}
}
Run Code Online (Sandbox Code Playgroud)
在你的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" />
Run Code Online (Sandbox Code Playgroud)
注意:
Assuming
你为take permission
.添加了代码Marshmallow
结果 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