【问题标题】:Choose an image from gallery and show it using an ImageView从图库中选择图像并使用 ImageView 显示它
【发布时间】:2017-12-04 15:31:37
【问题描述】:
private static int RESULT_LOAD = 1;
String img_Decodable_Str;


ImageView imageView = (ImageView) findViewById(R.id.Gallery);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            // Start the Intent
            startActivityForResult(galleryIntent, RESULT_LOAD);

        }
    });}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            img_Decodable_Str = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.Gallery);
            // Set the Image in ImageView after decoding the String
            imageView.setImageBitmap(BitmapFactory
                    .decodeFile(img_Decodable_Str));

        } else {
            Toast.makeText(this, "Hey pick your image first",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went embrassing", Toast.LENGTH_LONG)
                .show();
    }

}}

我有一个 ImageView。如果用户点击ImageView,他将能够向其中添加他选择的图像。当我单击 ImageView 时,我被重定向到图库,但是当我选择图像时,该图像未显示在 ImageView 中。我哪里出错了?

【问题讨论】:

    标签: android image android-intent onclick gallery


    【解决方案1】:

    短期内,替换:

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();
    
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            img_Decodable_Str = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.Gallery);
            // Set the Image in ImageView after decoding the String
            imageView.setImageBitmap(BitmapFactory
                    .decodeFile(img_Decodable_Str));
    

    与:

            Uri selectedImage = data.getData();
            imageView.setImageURI(selectedImage);
    

    稍后,使用 Picasso 或 Glide 等图像加载库,因为 setImageURI() 会在主应用程序线程上加载图像,只要该工作需要,它就会冻结您的 UI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多