Android 从图库中选择一张图片
Android pick an image from gallery
在片段中,我这样调用画廊意图:
// Create the Intent for Image Gallery.
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, MainActivity.LOAD_IMAGE_RESULTS);
在主要 activity 中,我使用以下代码处理结果:
public static int LOAD_IMAGE_RESULTS = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.w("reqestCode:",""+requestCode);
if(requestCode == LOAD_IMAGE_RESULTS && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
//Link to the image
final String imageFilePath = cursor.getString(0);
Log.w("ImageFile",imageFilePath);
cursor.close();
}
}
但是在 onActivityResult
中 requestCode 返回 196609 value.So 我的代码不是 working.How 我可以解决它吗?
看起来问题出在您创建的 Intent 上。尝试这样的事情:
pictureActionIntent = new Intent(
Intent.ACTION_GET_CONTENT, null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent,
GALLERY_PICTURE);
有关详细信息,请查看 this 答案。
你说你是从 Fragment
调用它的。你试过在两个地方都听 onActivityResult
吗? Activity
以及 Fragment
?值得一试。 :)
试试这个代码它会起作用
// 启动 activity
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
GALLERY_INTENT_CALLED);
//在activity结果得到位图
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == GALLERY_INTENT_CALLED) {
if (null == data)
return;
String selectedImagePath;
Uri selectedImageUri = data.getData();
// MEDIA GALLERY
Bitmap bitmap = BitmapFactory.decodeFile(ImageFilePath.getPath(
getApplicationContext(), selectedImageUri));
ivProfileImage.setImageBitmap(bitmap);
}
}
}
在片段中,我这样调用画廊意图:
// Create the Intent for Image Gallery.
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, MainActivity.LOAD_IMAGE_RESULTS);
在主要 activity 中,我使用以下代码处理结果:
public static int LOAD_IMAGE_RESULTS = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.w("reqestCode:",""+requestCode);
if(requestCode == LOAD_IMAGE_RESULTS && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
//Link to the image
final String imageFilePath = cursor.getString(0);
Log.w("ImageFile",imageFilePath);
cursor.close();
}
}
但是在 onActivityResult
中 requestCode 返回 196609 value.So 我的代码不是 working.How 我可以解决它吗?
看起来问题出在您创建的 Intent 上。尝试这样的事情:
pictureActionIntent = new Intent(
Intent.ACTION_GET_CONTENT, null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent,
GALLERY_PICTURE);
有关详细信息,请查看 this 答案。
你说你是从 Fragment
调用它的。你试过在两个地方都听 onActivityResult
吗? Activity
以及 Fragment
?值得一试。 :)
试试这个代码它会起作用 // 启动 activity
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
GALLERY_INTENT_CALLED);
//在activity结果得到位图
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == GALLERY_INTENT_CALLED) {
if (null == data)
return;
String selectedImagePath;
Uri selectedImageUri = data.getData();
// MEDIA GALLERY
Bitmap bitmap = BitmapFactory.decodeFile(ImageFilePath.getPath(
getApplicationContext(), selectedImageUri));
ivProfileImage.setImageBitmap(bitmap);
}
}
}