I need to send byte[] data
from Activity1
to Activity2
, in order to writedata("FileOutputStream.write(data)")
in a jpg file. My final .jpg file could exceed 1mb.
Activity1:
public void onPictureTaken(byte[] data, Camera camera) {
Log.w("ImageSizeMyApp", String.valueOf(data.length));
mCamera.startPreview();
Intent shareWindow = new Intent(Activity1.this, Activity2.class);
shareWindow.putExtra("photo",data);
startActivity(shareWindow);
closeCamera();
Log.w("CameraActivity:", "onPictureTaken");
}
In Activity2:
Bundle extras = getIntent().getExtras();
data = extras.getByteArray("photo");
I use Log.w("ImageSizeMyApp", String.valueOf(data.length));
to get this:
ImageSizeMyApp﹕ 446367 (this size sends to the next activity, and everything is good)
ImageSizeMyApp﹕ 577368 (this size closes my camera, and does not send to the next activity)
So 500kb is the limit dimension for Intent. Is there any other stable method to send my byte[]
larger than 500kb between activities?
Any reference or advice is welcome. Thanks in advance!
Update:
Could I make another class to store that byte[] array? Or is it better to use a static variable?
We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
You need to refer to the names of extra fields you have set while calling another activity. So Change this : Intent i=getIntent(); name=i. getStringExtra(tname); tid=i.
To pass the data through Intent we will use putExtra() method and in parameter, we will use Key-Value Pair. Now, where we have to mention putExtra() method? We have to add putExtra() method in onClick() as shown in the below code and in parameter we have to mention key and its value.
Activity 1 :
//creates the temporary file and gets the path
String filePath= tempFileImage(context,yourBitmap,"name");
Intent intent = new Intent(context, Activity2.class);
//passes the file path string with the intent
intent.putExtra("path", filePath);
startActivity(intent);
Use this method to create the file:
//creates a temporary file and return the absolute file path
public static String tempFileImage(Context context, Bitmap bitmap, String name) {
File outputDir = context.getCacheDir();
File imageFile = new File(outputDir, name + ".jpg");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e(context.getClass().getSimpleName(), "Error writing file", e);
}
return imageFile.getAbsolutePath();
}
Activity 2 :
//gets the file path
String filePath=getIntent().getStringExtra("path");
//loads the file
File file = new File(filePath);
Finally load the image:
Picasso.with(context).load(file).fit().centerCrop().into(imageView);
Or:
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageView.setImageBitmap(bitmap);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With