【问题标题】:pick an image from gallery in android studio?从 android studio 的画廊中挑选一张图片?
【发布时间】:2015-05-02 16:24:57
【问题描述】:

有人可以告诉我是什么问题,它不工作,所以请快速帮助我真正需要的:

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

        }
    }
}

【问题讨论】:

  • 请添加堆栈跟踪
  • 请解释“不工作”是什么意思。另请注意,使用setImageURI() 不是一个好主意。引用文档,“这会在 UI 线程上进行位图读取和解码,这可能会导致延迟打嗝”。有many image loading libraries for Android 可以在后台线程上加载你的图片。
  • 我正在做一个应用程序,当我选择时我想从图库中选择图像,所以它关闭了我的应用程序,这是什么问题
  • "it is close my app" -- 如果您的意思是您的应用程序正在崩溃,请使用 LogCat 检查与您的崩溃相关的 Java 堆栈跟踪:stackoverflow.com/questions/23353173/… 另外,请记住,有很多 Android 开发者支持网站,in a variety of languages
  • 没有人,已经关闭,不显示此消息

标签: android styles


【解决方案1】:

这对我有用。

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

【讨论】:

  • LOAD_IMAGE_RESULTS 是什么?
  • 您在请求活动时使用的 int startActivityForResult(i, RESULT_LOAD_IMAGE); ...只需给它一些值,例如 123456... 真的没关系
  • 我试过是同样的问题,我认为,如果你可以更改我的代码,我不会使用 RESULT_LOAD_IMAGE,这将是最好的
  • Bojan 它不工作,我可能需要任何授权
  • 您需要在清单中拥有读取权限。但是我找到了我使用的原始示例,希望它对您有所帮助programmerguru.com/android-tutorial/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 2021-04-26
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多