在两个活动之间传递位图

时间:2012-08-31 14:33:24

标签: android android-activity bitmap share

我一直试图解决这个问题2个小时,但我还没有找到解决方案。

我的活动可以选择使用相机API拍照或浏览图库中的图片。我可以获取图像并将其显示在相同的活动中,但问题是我在其他活动中也需要它。

据我所知,最好的方法是将图像存储在SD卡中,但我已经尝试过,我只会收到错误和错误。

你可以帮我一把吗?

谢谢!

    public void onClick(View arg0) {

    // TODO Auto-generated method stub
    switch(arg0.getId()){

        case R.id.save:

            if (et.getText() !=null && thumbnail != null){

                 TableRow tr = new TableRow(this);
                 ImageView view = new ImageView(this);
                 TextView view2 = new TextView(this);
                 Button view3 = new Button(this);
                 view3.setOnClickListener(this);



                 view3.setId(i);


                 view.setImageBitmap(thumbnail);
                 view.setPadding(1, 5, 0, 0);
                 view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

                 Calendar c = Calendar.getInstance();
                 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

                 titulo[i]=""+et.getText();
                 mensaje[i]=""+et1.getText();
                 fotos[i]=thumbnail;


                 view2.setText(titulo[i] +"                 "+ dateFormat.format(c.getTime()) );

                 view2.setGravity(Gravity.CENTER_VERTICAL);
                 view2.setPadding(5, 0, 0, 0);
                 i++;

                 view3.setGravity(Gravity.RIGHT);
                 view3.setGravity(Gravity.CENTER_VERTICAL);

                 DisplayMetrics metrics = new DisplayMetrics();
                 getWindowManager().getDefaultDisplay().getMetrics(metrics);

                 tr.addView(view, metrics.widthPixels/3, 150);
                 tr.addView(view2, metrics.widthPixels/2, 150);
                 tr.addView(view3, metrics.widthPixels/6, 20);
                 tl.addView (tr, 0);


                 final Toast toastMensaje = Toast.makeText(getApplicationContext(),
                            "Tu entrada se cargó correctamente", Toast.LENGTH_SHORT);
                    toastMensaje.setGravity(Gravity.CENTER, 0, 0);
                    toastMensaje.show();

                    et.setText("");
                    et1.setText("");
                    i1.setVisibility(View.GONE);


            }

            else{

                final Toast toastMensaje = Toast.makeText(getApplicationContext(),
                        "Tienes que añadir un título y una foto", Toast.LENGTH_LONG);
                toastMensaje.setGravity(Gravity.CENTER, 0, 0);
                toastMensaje.show();

            }


        break;

        case R.id.photo:

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);


        break;

        case R.id.gallery:

            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
             startActivityForResult(intent, TFRequestCodes);

        break;

        default:



            Intent openActivity = new Intent(Second.this, Recuerdos.class);
            openActivity.putExtra("titulo", titulo[arg0.getId()]);
            openActivity.putExtra("mensaje", mensaje[arg0.getId()]);
            openActivity.putExtra("URI", selectedImage);


            startActivity(openActivity);

        break;

    }

}



protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {  
        // do something  

        thumbnail = (Bitmap) data.getExtras().get("data");
        i1.setImageBitmap(thumbnail);



    }  

    else if (requestCode == TFRequestCodes && resultCode == RESULT_OK) {  
        // do something



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

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();


        thumbnail = BitmapFactory.decodeFile(filePath);
        i1.setImageBitmap(thumbnail);


    }  


}  

}

2 个答案:

答案 0 :(得分:4)

您宁愿传递图片URI而不是图像数据。

onActivityResult()方法中,您可以获取图片URI:

Uri imageUri = data.getData();

然后你可以在启动它时将这个uri传递给另一个活动:

    Intent intent = new Intent(context, MyNextActivity.class);
    intent.setData(imageUri);
    startActivity(intent);

在下一个活动中,您可以检索此URI:

Uri imageUri = getIntent().getData();

然后在ImageView中显示图像:

imageView.setImageURI(imageUri);

答案 1 :(得分:0)

尝试使用此代码,

在您的FirstActivity中,

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

        Intent intent = new Intent();
        intent.setClass(AndroidPassingBitmap.this, AndroidReceiveBitmap.class);
        intent.putExtra("Bitmap", bitmap);
        startActivity(intent);

在SecondActivity中,

Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
  ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview);

  viewBitmap.setImageBitmap(bitmap);