【发布时间】:2015-12-26 18:33:44
【问题描述】:
我从图库中获取了用户选择的图像并希望在图像视图中显示它
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
imageView.setImageBitmap(BitmapFactory
.decodeFile(selectedImagePath));
Log.i("SELECTED IMAGE: ", selectedImagePath);
}
}
}
/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// for other file managers
return uri.getPath();
}
有些图片的路径为:/storage/sdcard0/WhatsApp/Media/WhatsApp Images/IMG-20150407-WA0013.jpg 可以正常显示
还有一些图片提供了谷歌下载链接的路径。示例:https://lh3.googleusercontent.com/-9VYhxxxmZs/VdQnxxxo5I/AAAAxxxCpM/4B3s-xxoeI/I/2015-08-08.jpg(出于隐私原因,帖子中的路径已更改)
这些图像不会显示。
我就是这样一种情况,
我将 Uri 作为 content://com.sec.android.gallery3d.provider/picasa/item/5913341278276321746
并且 selectedImagePath 为 https://lh3.googleusercontent.com/-9VYhxxxmZs/VdQnxxxo5I/AAAAxxxCpM/4B3s-xxoeI/I/2015-08-08.jpg
如何将这些图像的 selectedImagePath 获取为手机中的路径而不是 Web 位置?
【问题讨论】:
-
您是否考虑使用第三方库,例如 Picasso (square.github.io/picasso) 或 Glide (github.com/bumptech/glide)?
-
@GuilhE 我认为它可以由android库本身处理
-
你可以,但它需要更多的代码。使用其中一个库(通常我使用 Picasso),这样做非常简单快捷,并且您已经实现了缓存系统等。试一试;)
-
@GuilhE 我会尝试这样做,但现在如何在现有代码中完成?
-
@GuilhE 需要对现有代码进行哪些更改?
标签: android android-imageview android-image android-bitmap bitmapfactory