在2个活动之间传递位图 [英] Pass a Bitmap between 2 activities

查看:112
本文介绍了在2个活动之间传递位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试解决这个问题2个小时了,但是我还没有找到解决方案.

I've been trying to solve this issue for 2 hours and i haven't found the solution yet.

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

My activity has the option of taking a photo with the camera API or to browse an image from gallery. I can get the image and display it in the SAME ACTIVITY but the problem is that i need it in other activity too.

据我所知,最好的方法是将图像存储在SD卡中,但我尝试这样做,但只会出错.

As far as i know,the best way to do this is by storing the image in the SD card but i've tried to do it and i only get errors and errors.

你能帮我一下吗?

谢谢!

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


    }  


}  

}

推荐答案

您宁愿传递图像URI,而不传递图像数据.

You'd rather pass image URI instead of image data.

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

In the onActivityResult() method you can get the image URI:

Uri imageUri = data.getData();

然后,您可以在启动它时将这个uri传递给其他活动:

Then you can pass this uri to the other activity when starting it:

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

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

And in the next activity you can retrieve this URI:

Uri imageUri = getIntent().getData();

然后在ImageView中显示图像:

And then to display the image in an ImageView:

imageView.setImageURI(imageUri);

这篇关于在2个活动之间传递位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆