从新的照片应用程序中挑选图像

时间:2013-12-12 04:22:01

标签: android android-intent

我使用以下Intent来允许用户选择图片

Intent pictureIntent = new Intent();
pictureIntent.setType("image/*");
pictureIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(pictureIntent, GALLERY_PICK_IMAGE_REQUEST);

获得结果后,我使用以下方法(using this method

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == GALLERY_PICK_IMAGE_REQUEST) {
        if (resultCode == mActivity.RESULT_OK) {
            Uri selectedImage = data.getData();
            Log.d(TAG, "Gallery image path = " + selectedImage.getPath());
            launchUploadImageActivity(getRealPathFromURI(mActivity, selectedImage));
        }
    }
}

private String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

当我使用新的照片应用程序选择图片时,我可以选择我认为是谷歌服务器上的照片,因为它会返回图片的网址,而不是文件路径:

日志输出:     图库图片路径= / 0 / https://lh6.googleusercontent.com/lRkls4SQwi_afJvjO5QChsWqRwTpDjg- ....

有什么办法可以强迫用户选择手机本地的图片吗?

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码选择图片

Intent pictureIntent = new Intent(MediaStore.ACTION_PICK, Images.Media.INTERNAL_CONTENT_URL);
startActivityForResult(pictureIntent, REQUEST_CHOOSE_IMAGE);

而不是

Intent pictureIntent = new Intent();
pictureIntent.setType("image/*");
pictureIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(pictureIntent, GALLERY_PICK_IMAGE_REQUEST);

答案 1 :(得分:0)

将此行添加到您的意图中:

pictureIntent.setData(Images.Media.INTERNAL_CONTENT_URI);