How to send large byte arrays between actions in Android?

I need to send byte[] data from Activity1 to Activity2 in order to write data("FileOutputStream.write(data)") to a jpg file. My .jpg file can exceed 1 mb.

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)); for this:

  1. ImageSizeMyApp: 446367 (this size is sent to the next activity, and everything is fine)

  2. ImageSizeMyApp: 577368 (this size closes my camera and does not send the next operation)

So 500kb is the size limit for Intent. Is there any other stable method for sending my byte[] larger than 500 kB in between?

Any links or tips are appreciated. Thank you in advance!

Update:

Can I create another class to store this byte [] array? Or is it better to use a static variable?

+5
source share
3 answers
  • Create a temporary file.
  • Pass the path to another action.
  • Get the path and upload the image.

Action 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 a 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(); } 

Action 2:

 //gets the file path String filePath=getIntent().getStringExtra("path"); //loads the file File file = new File(filePath); 

Finally, upload the image:

 Picasso.with(context).load(file).fit().centerCrop().into(imageView); 

Or:

 Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); imageView.setImageBitmap(bitmap); 
+4
source

I had the same problem. In short: subclass Application (or create an auxiliary singleton), use it ( <application android:name=".App" .../> in manifest.xml ) and save the image data there.

App.java fragment:

 public final class App extends Application { private static App sInstance; private byte[] mCapturedPhotoData; // Getters & Setters public byte[] getCapturedPhotoData() { return mCapturedPhotoData; } public void setCapturedPhotoData(byte[] capturedPhotoData) { mCapturedPhotoData = capturedPhotoData; } // Singleton code public static App getInstance() { return sInstance; } @Override public void onCreate() { super.onCreate(); sInstance = this; } } 

Camera activity saves data:

 private Camera.PictureCallback mJpegCaptureCallback = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // We cannot pass the large buffer via intent data. Use App instance. App.getInstance().setCapturedPhotoData(data); setResult(RESULT_OK, new Intent()); finish(); } }; 

Parent activity reads data:

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RequestCode.CAPTURE && resultCode == RESULT_OK) { // Read the jpeg data byte[] jpegData = App.getInstance().getCapturedPhotoData(); App.log("" + jpegData.length); // Do stuff // Don't forget to release it App.getInstance().setCapturedPhotoData(null); } } 
+3
source

Following are the steps:

1) Create a table in sqlite with columns of bytes and identifiers 2) Save this byte and its identifier (you will give) from Activity 1 3) Go to step 2 (do not forget to send the parameter identifier) ​​4) In step 2, get this parameter from the database goals and requests using id to get bytes.

Hope this helps.

0
source

All Articles