Reputation: 2088
I am trying to send an image taken by a camera from one activity to another activity. I have tried it by the following two methods(from stackoverflow)
Method 1:
Passing Image via Path :
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Pictures" + File.separator + "SanPics2";
Intent image_intent = new Intent(Snap_ImageView.this,FullScreen.class);
image_intent.putExtra("image_path" ,path );
System.out.println("Inent has passed the extras");
startActivity(image_intent);
On Receiving End(FullScreen.java) :
final String path = getIntent().getStringExtra("image_Path");
Bitmap org_bmp = BitmapFactory.decodeFile(path);
imageview.setImageBitmap(org_bitmap);
This method doesn't seem to set the image but it reaches the destination activity and shows a plain white background, weird !! The Logcat for method 1 is as follows :
Method 1 Logcat :
12-26 18:57:01.520: D/dalvikvm(6137): GC_FOR_ALLOC freed 897K, 35% free 9156K/13955K, paused 221ms, total 221ms
12-26 18:57:01.970: D/dalvikvm(6137): GC_CONCURRENT freed 88K, 11% free 40280K/45191K, paused 28ms+28ms, total 96ms
12-26 18:57:02.790: I/System.out(6137): /storage/sdcard0/Pictures/SanPics2
12-26 18:57:06.270: I/System.out(6137): Inent has passed the extra
12-26 18:57:06.280: I/Choreographer(6137): Skipped 302 frames! The application may be doing too much work on its main thread.
Doesn't report on any problem but at the same time doesn't set the image expected.
Method 2:
Passing Image via ByteArray :
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
On Receiving End(FullScreen.java) :
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
This method doesn't reach the destination activity at all.
Method 2 Logcat :
12-26 19:04:12.620: D/dalvikvm(7237): GC_FOR_ALLOC freed 1322K, 34% free 9244K/13955K, paused 144ms, total 169ms
12-26 19:04:12.860: D/dalvikvm(7237): GC_CONCURRENT freed 90K, 11% free 40366K/45191K, paused 25ms+14ms, total 73ms
12-26 19:04:14.350: I/System.out(7237): /storage/sdcard0/Pictures/SanPics2
12-26 19:04:19.850: I/System.out(7237): Inent has passed the extra
12-26 19:04:19.930: I/Choreographer(7237): Skipped 454 frames! The application may be doing too much work on its main thread.
This is as same as Method 1 Logcat. I am totally lost here cause neither of the methods work and I am unable to find whats happening inside. Any ideas would be highly appreciated. It'll be great if either one of the activity can be made to work.
NOTE :
I am calling both the styles of intent within the below depicted code :
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);
view_image.setImageBitmap(bitmap);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Pictures" + File.separator + "SanPics2";
System.out.println(path);
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System
.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
ByteArrayOutputStream byte_array_stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, byte_array_stream);
byte[] byte_array = byte_array_stream.toByteArray();
****Intent image_intent = new Intent(Snap_ImageView.this,FullScreen.class);
image_intent.putExtra("image_path" ,byte_array );//Similarly path intent style at the same position
System.out.println("Inent has passed the extra");
startActivity(image_intent);****
outFile.flush();
outFile.close();
}
Does this have anything to do with the restriction of image display at destination activity ? I am just a fresher so i apologize if these kinda questions sound silly to ya. Cheers !!
Upvotes: 0
Views: 486
Reputation: 2088
The problem was that I had not given the image extension along with the file name. That was the problem that I had. Method 1 works well if you add the following :
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Pictures" + File.separator + "SanPics2";
File file = new File(path,"Your image name.jpg");//**Extension is what matters**
String image_name = file.toString();
Intent image_intent = new Intent(Snap_ImageView.this,FullScreen.class);
image_intent.putExtra("image_path" ,image_name );
System.out.println("Inent has passed the extras");
startActivity(image_intent);
This has worked for me.
Upvotes: 0
Reputation: 5077
so what do you want? you have already saved photo and need to open it from another activity?
Put it to the indent and sent it
filepath = "Your Image Path"
intent.putExtra("imagePath", filepath);
Receive it from Another Activity
String image_path = getIntent().getStringExtra("imagePath");
Bitmap bitmap = BitmapFactory.decodeFile(image_path);
myimageview.setImageDrawable(bitmap);
and let me know if i got you wrong!
Edited:
As you reqested, convertion of Drawable from Bitmap
Bitmap image = BitmapFactory.decodeResource(context.getResources(), R.drawable.cam_image);
or try this
Bitmap bitmap = ((BitmapDrawable)ur_drawable).getBitmap();
Upvotes: 1