views:

105

answers:

1

I figured out how to pass a String value between activites thanks to this site, however I'm having trouble passing an image. What I'm trying to to is have a user click a button that opens the gallery and allows selecting of a picture. Then I have another button that opens another activity that displays an ImageView. I want to be able to have that ImageView's image be the chosen one from the previous activity.

Here is the class that has the button I'm clicking to open the gallery and retrieve the chosen image:

public class EnterEdit extends Activity implements View.OnClickListener 
{
private static final int SELECT_IMAGE = 0;

 String filepath;

 Bundle fieldresults;
 Intent b; 

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.enteredit);

  Button selectwallpaper = (Button) findViewById(R.id.selectwallpaper);
  selectwallpaper.setOnClickListener(this);

  Button previewwallpaper = (Button) findViewById(R.id.previewwallpaper);
  previewwallpaper.setOnClickListener(this);

  fieldresults = new Bundle();
  b = new Intent(this, PreviewScreen.class);
 }


 @Override
 public void onClick(View view) 
 {
  switch (view.getId())
  {
   case R.id.selectwallpaper:
    Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, SELECT_IMAGE);
    break;

   case R.id.previewwallpaper:
     startActivity(b); 
    }
    break;   
  }


 protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
  super.onActivityResult(requestCode, resultCode, data);

   if (resultCode == RESULT_OK)
   {
    if (requestCode == SELECT_IMAGE)
    {
     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]);
     filepath = cursor.getString(columnindex);
     cursor.close();

     fieldresults.putString("bitmap", filepath);
     b.putExtras(fieldresults);
    }
   }
 }
}

And here is the class that should display the chosen image:

public class PreviewScreen extends Activity implements View.OnClickListener 
{

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.previewscreen);

  Bundle fieldresults = this.getIntent().getExtras();
  String backgroundpath = fieldresults.getString("bitmap");

  String background = BitmapFactory.decodeFile(backgroundpath);
  ImageView gallerypic = (ImageView) findViewById(R.id.gallerypic);
  gallerypic.setImageBitmap(background); 
 }
}

What I'm not sure about is in the OnActivityResult if I should pass the selectedImage or the chosenimage in the b.putExtra("bitmap", selectedimage); line. I tried both but I didn't see an image on the second activity. Also I wasn't sure in the PreviewScreen class if I'm setting the imageview correctly. Any help is appreciated. Thanks.

+1  A: 

Trust me, you don't want to be passing images between activities. Rather, why don't you simply pass the path of the image and let the second activity decide what to do with that path (decode and display the image, in this case).

Cristian
So I'd pass the "filepath" variable (the one from the lineString filepath = cursor.getString(columnindex) )
jmrmb80
Yes... and then, you decode the image in the activity B. You alredy know how to do that :)
Cristian
OK I made some edits but the ImageView is still blank in the second activity. Am I passing it correctly in the first class and referencing it correctly in the second one? Thanks for the help so far.
jmrmb80
After doing some digging I found that if I click the button and chose an image (say imageA) from the gallery I click the button go to the next activity and I don't see an image. However if I go back to the previous activity and choose another image (say imageB), then I'll see imageA on the next activity. Then if I repeat the above, then I'll see imageB on the ImageView. If anyone can tell me why this is happening it would be much appreciated. Thanks.
jmrmb80
Could you please share the code you are using to get that behavior?
Cristian
Finally figured out the problem. Thanks for the help earlier.
jmrmb80
I edited the above code to what I have have that is working. I just had to move 2 lines of code (moved the last 2 lines that are now in the onActivityResult function from where they were previously which was in the selectwallpapercase statement).
jmrmb80