Android - how can i transfer ImageView from one ac

2019-02-28 20:06发布

问题:

I have Activity1 which have one ImaveView including image preview. Once i press the button i go from Activity1 to Activity2. In Activity2 i do not have image preview but an option button "Save the image of Activit1?" YES or NO.

Currently i am doing in wrong way which is like saving the image in disk and then reading it back from disk. But is there any way without saving the image i can transfer one imageView from Activity1 to Activity2 ?

Here is how i get the picture in my Activity1 > ImageView, which then need to be moved to Activity2. Any idea?

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      this.imageView = (ImageView)this.findViewById(R.id.picture);            
      Button photoButton = (Button) this.findViewById(R.id.capture_btn);      
      Button btnShareToEmail = (Button) this.findViewById(R.id.btnshare);
      btnShareToEmail.setOnClickListener(this);

      //photoButton.setOnClickListener(new View.OnClickListener() {
          //@Override
        //  public void onClick(View v) {
          //    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            //  startActivityForResult(cameraIntent, CAMERA_REQUEST); 
         // }
      //});

      // without frozen
      new Handler().postDelayed(new Runnable() { public void run() { 
          Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
          startActivityForResult(cameraIntent, CAMERA_REQUEST);
      }}, 100);      
  }



  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);



    }  
  }

EDIT:

// Set - Activity1

Intent winShare = new Intent(getBaseContext(), Activity2.class);
winShare.putExtra("Title", "r2.jpg");
winShare.putExtra("image1", photo);         
//int image_link = getIntent().getIntExtra("image1");
startActivityForResult(winShare,0); 

// GET - Activity2

    Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("image1"); // BITMAP_SHARED_KEY = "bitmap_shared_key"

    imageView.setImageBitmap(bitmap);
    // save it
    imageView.buildDrawingCache();
    Bitmap bm=imageView.getDrawingCache();
    OutputStream fOut = null;
    Uri outputFileUri;
    try {
      File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MYAPPPPPPPPPS" + File.separator);
      root.mkdirs();
      File sdImageMainDirectory = new File(root, "myPicName.jpg");
      outputFileUri = Uri.fromFile(sdImageMainDirectory);
      fOut = new FileOutputStream(sdImageMainDirectory);
      bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
      fOut.flush();
      fOut.close();      
   } catch (Exception e) {
     Toast.makeText(this, "Error occured. Please try again later.",Toast.LENGTH_SHORT).show();
   }

回答1:

There are many way to solve this problem. And here are 2 simple ways:

The first, you can read more about SharedPreferences

The second, you can putExtra bitmap from this Activity to another activity like this:

putExtra:

intent.putExtra(BITMAP_SHARED_KEY, yourBitmap);
startActivity(intent);

getBitmap which you've shared

Bitmap bitmap = (Bitmap) intent.getParcelableExtra(BITMAP_SHARED_KEY); // BITMAP_SHARED_KEY = "bitmap_shared_key"


回答2:

First of all you need to understand that when you are getting an image using the camera activity this way you are getting a thumbnail version of the taken image. in order to get a full size image it has to be saved, you can take a look at this blog post I wrote on this matter for more information:

Use Camera Activity for Thumbnail and Full Size Image

Now for your question, I think that the best option would be to save this image to a file (described in the guide) and pass an extra to the following activity with the string path of the image.



回答3:

Simple Way Use this

  1. First Activity

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
    R.drawable.men_icon);
    Bundle extras = new Bundle();
    Intent intent=new Intent(FirstActivity.class,SecondActivity.class);
    extras.putParcelable("Bitmap", bitmap);
    intent.putExtras(extras);
    startActivity(intent);
    
  2. Second Activity

     ImageView iv_photo=(ImageView)findViewById(R.id.iv_photo);
     Bundle extras = getIntent().getExtras();
     Bitmap bmp = (Bitmap) extras.getParcelable("Bitmap");
     iv_photo.setImageBitmap(bmp);