简体   繁体   English

如何从图库中获取图像

[英]how to get image from gallery

I'm trying to get image from the gallery of my device ... I'm having a dialog that has a button that initializes the gallery after the user chooses a photo the path of it should be returned ... 我正在尝试从设备的图库中获取图像...我有一个对话框,该对话框带有一个按钮,可在用户选择照片后初始化图库,并应返回其路径...

but the path is returned as null 但路径返回为空

that's how I do it - 我就是这样的-

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);

In my onActivityResult - 在我的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();
            }
        }
    }

And that's my getPath function - 那就是我的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);
}

Few months ago I faced a similar situation. 几个月前,我遇到了类似的情况。 You cannot assume that the Uri returned by the media picker will correspond to a local file , so the best approach is to use a ContentResolver to treat the picture as a stream. 您不能假设媒体选择器返回的Uri将对应于本地文件,因此最好的方法是使用ContentResolver将图片视为流。

First of all open the gallery activity with some request code (0 for this example) 首先,打开带有一些请求代码的图库活动(此示例为0)

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

Then override the onActivityResult method to get the bitmap from gallery. 然后重写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); 
}

Call intent as below: 呼叫意图如下:

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

Correct OnActivityResult as below: 更正如下所示的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));
        }
    }

Source: Android get image from gallery into ImageView 资料来源: Android将图片从图库导入ImageView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM