Passing image to another activity

All your problems with Audio, Video and Images.

Passing image to another activity

Postby jmrmb80 » Tue Aug 24, 2010 6:14 am

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:
Code: Select all
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);
    fieldresults.putString("bitmap", filepath);
    b.putExtras(fieldresults);
    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();
    }
   }
}
}


And here is the class that should display the chosen image:
Code: Select all
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);
}
}


After 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
Freshman
Freshman
 
Posts: 2
Joined: Mon Aug 23, 2010 12:38 am

Top

Re: Passing image to another activity

Postby blundell » Fri Aug 27, 2010 12:19 pm

There is an easier way to do this :-)

Code: Select all
Log.i(MainActivity.TAG, "Select Gallery Image");
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, GALLERY_ACTIVITY);


You then capture the result:
Code: Select all
@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
                super.onActivityResult(requestCode, resultCode, intent);
                Log.d(MainActivity.TAG, "onActivityResult() Result Code: " + resultCode);
                if (resultCode == RESULT_CANCELED) {
                        // TODO // Atm just loads defaults meh
                        return;
                }
                switch (requestCode) {
                        case GALLERY_ACTIVITY:
                                Log.d(MainActivity.TAG, "Got a gallery result");
                                // Get the gallery data
                                String galleryImageURI = intent.getData().toString();
// Do whatever you want here I save the URI to the sharedpreferences
                                savedPreferences.edit().putString(MainActivity.IMAGE_BACKGROUND_URL, galleryImageURI).commit();
                                break;
                }
        }
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Passing image to another activity

Postby jmrmb80 » Fri Aug 27, 2010 7:19 pm

Thanks for the suggestion. I actually got my original way to work.

What benefit does saving to sharedpreferences do?
jmrmb80
Freshman
Freshman
 
Posts: 2
Joined: Mon Aug 23, 2010 12:38 am

Re: Passing image to another activity

Postby blundell » Sat Aug 28, 2010 11:59 am

Well if you save it to the shared preference, you can recall it after the app has been closed and opened again
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 21 guests