In my app i am displaying no.of images from gallery from where as soon as I select one image , the image should be sent to the new activity where the selected image will be set as background.However, I am able to get the images from gallery but as soon as I select one the application crashes.Thanks in advance
Activity-1(The images are shown in gallery and the selected image is sent to new activity)
public class Gallery extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String tmppath = cursor.getString(column_index);
Bitmap croppedImage = BitmapFactory.decodeFile(tmppath);
// Bitmap croppedImage = BitmapFactory.decodeFile(croppedImage);
Intent intent = new Intent(Gallery.this,GesturesActivity.class);
intent.putExtra("bmp",croppedImage);
startActivity(intent);
Log.v("sending image","sending image");
}
}
}
Activity-1(XML)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
>
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_weight="1" android:layout_height="wrap_content"></ImageView>
<Button
android:layout_height="wrap_content"
android:text="Load Picture"
android:layout_width="wrap_content"
android:id="@+id/buttonLoadPicture"
android:layout_weight="0"
android:layout_gravity="center"></Button>
</LinearLayout>
Activity-2(The activity where the selected image should be set as background image of the screen)
public class GesturesActivity extends Activity {
private final int MENU_CAMERA = Menu.FIRST;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Bitmap bmp = (Bitmap)this.getIntent().getParcelableExtra("bmp");
BitmapDrawable background = new BitmapDrawable(bmp);
getWindow().setBackgroundDrawable(background); //background image of the screen
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.advert);
View view = new SandboxView(this, bitmap);
setContentView(view);
}
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
menu.add(0, 11, 0, "Take Snapshot");
return super.onPrepareOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
There are 3 Solutions to solve this issue.
1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.
Convert Bitmap to Byte Array:-
Pass byte array into intent:-
Get Byte Array from Bundle and Convert into Bitmap Image:-
2) First Save image into SDCard and in next activity set this image into ImageView.
3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.
IGP summed it up clearly, but in my opinion the most efficient way to do it is by passing the URI of the image to the next activity, instead of the Bitmap itself. I'm actually not sure if it's possible to pass whole Bitmaps (or, converted, ByteArrays) of data from one activity to another using Intents - I believe there is a limit as to how much data a Bundle can contain.
Instead pass the reference you're using to display the image in the first activity. I assume you're using some kind of lazy loading? If not, I highly suggest you do. This way you can simply re-query for the Bitmap via the URI.
I'm still puzzled as to how these kinds of problems reach SO. Check the logs, maybe you can figure it out on your own.