[英]Some images are coming blank when picking from gallery in android
I am working on an application where I am trying to load images to my application through gallery. 我正在尝试通过图库将图像加载到应用程序的应用程序。 I am using
Intent
to get the image from the gallery. 我正在使用
Intent
从图库中获取图像。 The images stored in internal storage are coming perfectly in the application, but the images stored in external storage are coming blank. 内部存储中存储的图像在应用程序中完美呈现,但是外部存储中存储的图像为空白。
Code to get images from gallery: 从图库获取图像的代码:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
This is the code to get the bitmap and absolute path(getPath() returns the absolute path of the image) of the image: 这是获取位图和代码的绝对路径(getPath()返回图像的绝对路径)的代码:
Bitmap bm = null;
String selectedImagePath = null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
bm = scaleDownLargeImageWithAspectRatio(bm);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 90, bytes);
byte[] byteArray = bytes.toByteArray();
strProfileImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
selectedImagePath = getPath(HomeScreenActivity.this, data.getData());
Log.e("ImagePath Gallery", data.getData().toString());
} catch (IOException e) {
e.printStackTrace();
}
if (selectedImagePath != null) {
Uri currentImageUri = null;
if (Build.VERSION.SDK_INT >= 24) {
// wrap File object into a content provider. NOTE: authority here should match authority in manifest declaration
currentImageUri = FileProvider.getUriForFile(HomeScreenActivity.this, "com.example.sravanthiv.consumermobileapp_nonclinical_android.fileprovider", new File(selectedImagePath)); // use this version for API >= 24
} else {
currentImageUri = Uri.fromFile(new File(selectedImagePath));
}
}
I am using the currentImageUri
to load the images in my application. 我正在使用
currentImageUri
在应用程序中加载图像。 But some are coming blank(stored in external storage) and some are coming perfect(stored in internal storage). 但是有些变的很空白(存储在外部存储中),有些变的很完美(存储在内部存储中)。
I am using Picasso to load images: Picasso.with(mContext).load(Uri.parse(imagesList.get(currentPosition))).resize(135,135).centerCrop().into(holder.selfie_Imageview);
我正在使用Picasso加载图像:
Picasso.with(mContext).load(Uri.parse(imagesList.get(currentPosition))).resize(135,135).centerCrop().into(holder.selfie_Imageview);
Its not able to load this path: content://com.example.android.fileprovider/all_dirs/DCIM/Camera/IMG_20171226_161309.jpg
它无法加载此路径:
content://com.example.android.fileprovider/all_dirs/DCIM/Camera/IMG_20171226_161309.jpg
But its able to load this path: content://com.example.android.fileprovider/all_dirs/IMG_20171226_161309.jpg
但它能够加载此路径:
content://com.example.android.fileprovider/all_dirs/IMG_20171226_161309.jpg
Try this one solution: 试试这个解决方案:
private void galleryIntent() {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, Util.SELECT_FILE);
}
onActivityResult
do this : onActivityResult
这样做:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == Util.SELECT_FILE) {
onSelectFromGalleryResult(data);
}
}
}
Get result from gallery selected image 从图库选择的图像中获取结果
private void onSelectFromGalleryResult(Intent data) {
if (data != null) {
Uri selectedImageUri = data.getData();
getImageRealPath = getRealPathFromURI(selectedImageUri);
if (getImageRealPath == null) {
// need to code download google photo code
}
file = new File(getImageRealPath);
Glide.with(AddNewPostActivity.this).load(getImageRealPath).into(imageView);
}
}
Forgetting image real path 忘记图像的真实路径
public String getRealPathFromURI(Uri uri) {
try {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Hope this help you...if you need any help you can ask 希望对您有帮助...如果您需要任何帮助,可以询问
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.