简体   繁体   English

如何将捕获的/画廊图像传递给android中的下一个活动

[英]how to pass captured/gallery image to next activity in android

I want to pass captured camera image and select image from gallery, image should be display in next activity for captured image my code is working fine..but select image from gallery the image is not displaying in the activity.. 我想传递捕获的相机图像并从图库中选择图像,应在下一个活动中显示该图像以获取捕获的图像,我的代码工作正常..但是从图库中选择图像但该图像未显示在活动中。

First Activity 第一次活动

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {

            Uri 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]);
            //file path of captured image
            imagepath = cursor.getString(columnIndex); 
            //file path of captured image
            File f = new File(imagepath);
            String filename = f.getName();

            /*Toast.makeText(Contact.this, "Your Path:"+imagepath, 2000).show();
            Toast.makeText(Contact.this, "Your Filename:"+filename, 2000).show();*/
            cursor.close();
            //Log.i("image path", imagepath);
            Bitmap imageData = BitmapFactory.decodeFile(imagepath);
            // Bitmap imageData = null;
             imageData = (Bitmap) data.getExtras().get("data");

             Intent i = new Intent(this, Cam.class);
             i.putExtra("name", imageData );
             startActivity(i);
            //imageview.setImageBitmap(bit);
        }



        else if (requestCode == SELECT_FILE) {
                //Bitmap photo = (Bitmap) data.getData().getPath(); 

                Uri selectedImageUri = data.getData();
                imagepath = getPath(selectedImageUri, null);

                 // your bitmap
                //ByteArrayOutputStream bs = new ByteArrayOutputStream();
                Bitmap imageData = BitmapFactory.decodeFile(imagepath);                                                                                               
                imageData = (Bitmap) data.getExtras().get("data");
                Intent intent = new Intent(this, Cam.class);
                intent.putExtra("name", imageData );
                 startActivity(intent);

Second Activity 第二次活动

protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.cam);

      Bitmap bitmap  = getIntent().getExtras().getParcelable("name");
        ImageView view = (ImageView) findViewById(R.id.imageView1);
        view.setImageBitmap(bitmap); 



        }
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

and retrieve it on the other end: 并在另一端检索它:

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

To start another activity and receive a result back call startActivityForResult() (instead of startActivity() ). 要启动另一个活动并接收结果,请调用startActivityForResult() (而不是startActivity() )。

In the startActivityForResult pass the image URI and the next context. 在startActivityForResult中,传递图像URI和下一个上下文。

I think this should be enough in your scenario. 我认为这在您的情况下就足够了。

您可以使用包传递给另一个活动。

If the image in your device, you can pass path of the image. 如果图像在您的设备中,则可以传递图像的路径。 Than display from path. 比从路径显示。

Use this. 用这个。

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("path", imagepath);

in your next Activity retrieve as 在您的下一个活动中检索为

String image_path = getIntent().getStringExtra("path");
Bitmap bitmap = BitmapFactory.decodeFile(image_path);
imageview.setImageBitmap(bitmap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM