我正在尝试使用以下代码从图库中获取图像:
private void initGalleryFetchImage() {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
LOGI(TAG, "picturePath: " + picturePath);
if (picturePath.startsWith("http")) {
Picasso.with(getActivity()).load(picturePath).into(iv);
} else {
Picasso.with(getActivity()).load(new File(picturePath)).into(iv);
}
}
}
我可以检索除拍摄之外的所有图像 并由相机应用程序存储。
[☓] picturePath: /storage/emulated/0/DCIM/Camera/20141223_102304.jpg
[✓] picturePath: https://lh4.googleusercontent.com/...f3rf3.jpg
[✓] picturePath: /storage/emulated/0/DCIM/Facebook/IMG_13280625132331.jpeg
[✓] picturePath: /storage/emulated/0/Pictures/Instagram/IMG_20140521_232443.jpg
[✓] picturePath: /storage/emulated/0/LINEcamera/2014-04-12-19-47-52_deco.jpg
[✓] picturePath: /storage/emulated/0/foursquare/foursquare/1403867797152.jpg
有谁知道如何解决这个问题?
答案 0 :(得分:0)
使用selectedImage
Uri
来加载图片
if (picturePath.startsWith("http")) {
Picasso.with(getActivity()).load(picturePath).into(iv);
} else {
Picasso.with(getActivity()).load(selectedImage).into(iv);
}
答案 1 :(得分:0)
try this
private void initGalleryFetchImage() {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK ) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
try {
bmp = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(selectedImage));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}