【问题标题】:How to send image from one activity to another activity that is selected from the android Gallery?如何将图像从一个活动发送到从 android Gallery 中选择的另一个活动?
【发布时间】:2011-12-21 23:24:34
【问题描述】:

我想设置要从 Android 图库中选择的图像。 我使用此代码来获取选定的图像。

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

而onActivityResult方法是这样的:

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
        super.onActivityResult(requestCode, resultCode, data);     
        if (requestCode == 10 && resultCode == Activity.RESULT_OK) {             
            Uri contentUri = data.getData();          
            String[] proj = { MediaStore.Images.Media.DATA };         
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
            cursor.moveToFirst();         
            String tmppath = cursor.getString(column_index);           
            Bitmap croppedImage = BitmapFactory.decodeFile(tmppath);
            go.setVisibility(View.VISIBLE);
            //canvas.drawBitmap(kangoo, 130, 100, null);
            //previewImage.setVisibility(View.VISIBLE);

            imageSrc.setImageBitmap(croppedImage);    //set to your imageview          
        }
}

现在,我想从图库中选择图像并将其发送到另一个活动。那么上面的代码怎么可能? 谢谢。

【问题讨论】:

    标签: android android-layout android-emulator android-widget android-ndk


    【解决方案1】:

    您可以将图像的URI 传递给下一个Activity

    你从onActivityResult()得到的URI

    onCreate() 的下一个Activity

    再次解码Bitmap并将其设置为ImageView

    【讨论】:

      【解决方案2】:

      将图像从一个活动传递到另一个活动过于昂贵,而不是您可以将其图像路径作为字符串传递并加载。

      this 帖子

      【讨论】:

        【解决方案3】:

        在 OnActivityResult 中

        Intent intent = new Intent(Activity1.this,Activity2.class);
        intent.putExtra("bmp",croppedImage);
        startActivity(intent);
        

        第二个活动

        Bitmap bmp = this.getIntent().getParcelableExtra("bmp");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-11-04
          • 2012-07-20
          • 1970-01-01
          • 2016-08-18
          • 2014-11-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多