【问题标题】:Android select image from gallery, get image id, pass image id to another activity and load image to imageviewAndroid从图库中选择图像,获取图像ID,将图像ID传递给另一个活动并将图像加载到imageview
【发布时间】:2014-10-13 00:16:19
【问题描述】:

正如标题所言。

这就是我想要做的事情

1) 打开图库(我知道怎么做) 2)从图库中选择图像 3)获取Image Id并将ID传递给另一个Activity 4)在这个其他活动中,我想将此图像加载到 ImageView 中。

我知道我可以这样做

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

然后将数组传递给意图

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

然后从Bundle中获取Byte Array并转换成Bitmap Image

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

但我相信通过传递 ID 然后在下一个活动中获取 id 并从 SD 卡加载图像会更好地提高内存和性能。

【问题讨论】:

    标签: android image android-intent bitmap android-gallery


    【解决方案1】:

    我有一些来自不久前从事的项目的代码 sn-ps。也许这会有所帮助: (对不起,如果某些代码不相关,正如我在我的一个旧项目中所说的那样)

    首先当然是找到图片

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

    当用户选择一张图片时,onActivityResult 将被调用并将用户带回到之前的 Activity(大部分时间是它启动的)。在这里,我们可以获得一些可以传递给下一个 Activity 的数据,例如图像 URI。

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
    
            switch (requestCode) {
    
            case SELECT_PICTURE:
                // Uri selectedImageUri;
                selectedImageUri = data.getData();
    
                if (pic.getDrawingCache() != null) {
                    pic.destroyDrawingCache();
                }
    
                pic.setImageURI(selectedImageUri);
                pic.buildDrawingCache(true);
                pic.setDrawingCacheQuality(ImageView.DRAWING_CACHE_QUALITY_HIGH);
                pic.setMaxHeight(pic.getDrawable().getBounds().width());
    
                tvPicInfo
                        .setText(String.valueOf(pic.getDrawable().getBounds()
                                .width())
                                + "px"
                                + " x "
                                + String.valueOf(pic.getDrawable().getBounds()
                                        .height()) + "px");
    
                break;
    
            case 2:
    
                selectedImageUri = data.getData();
    
                if (pic.getDrawingCache() != null) {
                    pic.destroyDrawingCache();
                }
    
                pic.setImageURI(selectedImageUri);
                pic.buildDrawingCache(true);
                pic.setDrawingCacheQuality(ImageView.DRAWING_CACHE_QUALITY_HIGH);
                break;
    
            }
    
        }
    }
    

    现在我们有了所选图像的 uri,此方法可以获取它的路径,您可以将其传递给 bundle 中的下一个活动。

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

    在下一个活动中,从包中加载路径,就像您为字节数组一样。使用该路径,使用 BitmapFactory 进行解码

    Bitmap bitmap = BitmapFactory.decodeFile(path);
    

    希望这对您的旅程有所帮助,Happy Coding!

    编辑:否则 { how to send imageview from one activity to other } 即使这看起来几乎是你不想要的

    【讨论】:

    • 在 pic.getDrawingCache() 中,“pic”是什么?
    • "pic" 将是主题中的 ImageView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多