简体   繁体   中英

Sending a Byte Array via Intent.putExtra does not work

I'm trying to send a captured image's Byte Array data to another view via the Intent putExtra method however it only works for the front camera (since the quality is lower than the back cam). Pictures captured via the back camera takes ages to be displayed and most of the time crashes the app.

I tried compressing too, but that didn't do the trick either. Have a look at my code:

captureImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCamera.takePicture(null, null, new PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {

                            Intent i = new Intent(CameraActivity.this, ImageEditor.class);
                            i.putExtra("image", data);
                            startActivityForResult(i, 0);

                         }
                });
            }
        });

The second activity doesn't get opened and nor does the app respond after capturing the image (via the back camera). This however works for the front camera, since I'm guessing the byte array is comparatively low in size and needs less memory than the pictures captured by the back camera.

How do I resolve this issue so that I can send the image captured in the Activity A to Activity B?

How do I resolve this issue so that I can send the image captured in the Activity A to Activity B?

The best answer is: don't do it at all. If those pieces of UI are that tightly coupled, they should be one activity, perhaps using multiple fragments.

The next-best answer is: carefully pass it via a static data member (eg, Activity A puts the byte[] in a static data member, where Activity B obtains it from). You need to make sure that you do not leak this byte[] , by setting the data member to null as soon as Activity B has a reference to that data.

First try (testing purposes only, if possible):

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="package_name"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:name="app_name"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:largeHeap="true" <!-- large heap --> 
        android:theme="@style/AppTheme" >
</manifest>

From https://developer.android.com/training/articles/memory.html :

Check how much memory you should use However, the ability to request a large heap is intended only for a small set of apps that can justify the need to consume more RAM (such as a large photo editing app). Never request a large heap simply because you've run out of memory and you need a quick fix—you should use it only when you know exactly where all your memory is being allocated and why it must be retained. Yet, even when you're confident your app can justify the large heap, you should avoid requesting it to whatever extent possible. Using the extra memory will increasingly be to the detriment of the overall user experience because garbage collection will take longer and system performance may be slower when task switching or performing other common operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM