如何从画廊获取图像

时间:2015-02-15 19:44:37

标签: android image-gallery onactivityresult

我正试图从我的设备库中获取图像...... 我有一个对话框,其中有一个按钮,用于在用户选择照片后初始化图库,应该返回它的路径...

  

但路径返回为null

我就是这样做的 -

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"),
                        SELECT_PICTURE);

在我的onActivityResult -

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                photo_path = getPath(selectedImageUri);
                // photo_path=selectedImageUri.getPath();
                Globals.galleryFlag = true;
                DialogFragment pindiDialogFragment = new PinPhotoDialogFragment();
                pindiDialogFragment.show(getFragmentManager(), "pin photo");
                dismiss();
            }
        }
    }

这是我的getPath功能 -

    public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity.getContentResolver().query(uri,
            projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

2 个答案:

答案 0 :(得分:1)

几个月前,我遇到了类似的情况。您不能假设媒体选择器返回的Uri将对应于本地文件,因此最好的方法是使用ContentResolver将图片视为流。

首先用一些请求代码打开gallery活动(本例中为0)

 Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
 startActivityForResult(i, 0);

然后覆盖onActivityResult方法以从图库中获取位图。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 0 && resultCode == RESULT_OK && null != data) { // we have bitmap from filesystem!
        Uri selectedImage = data.getData();

        InputStream inputStream = null;

        if (ContentResolver.SCHEME_CONTENT.equals(selectedImage.getScheme())) {
            try {
                inputStream = this.getContentResolver().openInputStream(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            if (ContentResolver.SCHEME_FILE.equals(selectedImage.getScheme())) {
                try {
                    inputStream = new FileInputStream(selectedImage.getPath());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            rpView.setReportPicture(bitmap,false); 
}

答案 1 :(得分:0)

调用意图如下:

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);

更正OnActivityResult如下:

 @Override
    protected 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 = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

来源:Android get image from gallery into ImageView