在Android Studio中从相册选择图片?

6

有人能告诉我问题出在哪里吗?它不工作,所以请快点帮忙,我真的很需要。

 imagePick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Contact Image"),1);
        }
    });


  public void onActivityResult(int reqCode, int resCode, Intent data)
{
    if(resCode==RESULT_OK)
    {
        if(reqCode==1) {
            imageURI=data.getData();
            iv.setImageURI(data.getData());

        }
    }
}

请添加堆栈跟踪信息。 - Sebastian Walla
1
请解释一下“不起作用”的含义。同时请注意,使用setImageURI()不是一个好主意。引用文档中的话,“这会在UI线程上进行位图读取和解码,可能会导致延迟问题”。有许多适用于Android的图像加载库(https://android-arsenal.com/tag/46),可以在后台线程上加载您的图像。 - CommonsWare
我正在开发一个应用程序,想从图库中选择图片,但当我选择时,应用程序就会关闭,这是什么问题? - Nir Ben Yossef
如果您的应用程序崩溃了,可以使用LogCat来检查与崩溃相关的Java堆栈跟踪:https://dev59.com/iGAg5IYBdhLWcg3wpMSz。此外,请记住,有许多Android开发者支持网站,[以各种语言提供服务](http://www.andglobe.com)。 - CommonsWare
不要显示此消息,它是关闭的。 - Nir Ben Yossef
1个回答

11

这对我起作用。

private final static int SELECT_PHOTO = 12345;

 imagePick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });    

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

        // Here we need to check if the activity that was triggers was the Image Gallery.
        // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
        // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
        if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) {
            // Let's read picked image data - its URI
            Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
            imageView.setImageBitmap(bitmap);

             // Do something with the bitmap


            // At the end remember to close the cursor or you will end with the RuntimeException!
            cursor.close();
        }
    }

1
加载图像结果是什么? - Nir Ben Yossef
当您请求活动 startActivityForResult(i, RESULT_LOAD_IMAGE); 时使用的 int ... 只需为其赋一个值,如 123456... 真的没有关系。 - Bojan Kseneman
我尝试了,但是问题还是一样的。我认为,我对RESULT_LOAD_IMAGE不太理解。如果您能更改我的代码,那将是最好的。 - Nir Ben Yossef
Bojan它不起作用了,我可能需要任何授权。 - Nir Ben Yossef
您需要在清单文件中拥有读取权限。不过,我找到了我当时使用的原始示例,希望它能对您有所帮助。http://programmerguru.com/android-tutorial/how-to-pick-image-from-gallery/ - Bojan Kseneman
问题不同。 - Nir Ben Yossef

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接