I am newbie when it comes to programming android. I am transferring images between the activity using both the ways i.e by using intent or by creating a file but it takes about 3 or 4 seconds to transfer image and show in imageview of second activity. Is there any way I can transfer faster then this because many application such as whatsapp are transferring at more faster rate. My code is given below. any help would be appreciated.
Code In first activity:
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.stopPreview();
Intent myintent = new Intent(CameraSetter.this,CameraPhotoViewer.class);
Bitmap bitmap_image = BitmapFactory.decodeByteArray(data, 0, data.length);
String fileName = "myImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap_image.compress(Bitmap.CompressFormat.JPEG, 50, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
startActivity(myintent);
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
}
};
and in second:
Bitmap bitmap_image = BitmapFactory.decodeStream(getApplicationContext().openFileInput("myImage"));
imageview.setImageBitmap(bitmap_image);
It is working but I want more faster way any ideas?
Also tried with saving the image to internal storage but it also taking too much time.
code is:
In first activity:
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.stopPreview();
Intent myintent = new Intent(CameraSetter.this, CameraPhotoViewer.class);
Bitmap bitmap_image = BitmapFactory.decodeByteArray(data, 0, data.length);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath = new File(directory, "profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmap_image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
startActivity(myintent);
System.out.print(directory.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
and in Second Activity:
imageview = (ImageView) findViewById(R.id.imview_camera_setter);
framelayout = (FrameLayout) findViewById(R.id.frame_layout_viewer);
try {
File f=new File(internalpath, "profile.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
imageview.setImageBitmap(Bitmap.createScaledBitmap(b, 300, 300, false));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
Still takes too much time