如何在Android中的活动之间发送大字节数组?

Chr*_*ris 5 android byte jpeg image android-activity

我需要发送byte[] dataActivity1Activity2,为了写data("FileOutputStream.write(data)")一个JPG文件.我的最终.jpg文件可能超过1mb.

活动1:

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");

}
Run Code Online (Sandbox Code Playgroud)

在Activity2中:

Bundle extras = getIntent().getExtras();
data = extras.getByteArray("photo");
Run Code Online (Sandbox Code Playgroud)

Log.w("ImageSizeMyApp", String.valueOf(data.length));用来得到这个:

  1. ImageSizeMyApp:446367(此大小发送到下一个活动,一切都很好)

  2. ImageSizeMyApp:577368(此尺寸关闭我的相机,不会发送到下一个活动)

所以500kb是Intent的限制维度.有没有其他稳定的方法来发送我的byte[]大于500kb的活动?

欢迎任何参考或建议.提前致谢!

更新:

我可以创建另一个类来存储byte []数组吗?或者使用静态变量更好?

Sof*_*ner 6

我有同样的问题。简而言之:子类化Application(或创建一个辅助单例),使用它(<application android:name=".App" .../>in manifest.xml)并将图像数据存储在那里。

App.java片段:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

相机活动保存数据:

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

父活动读取数据:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

  1. 创建一个临时文件.
  2. 将文件路径传递给Other Activity.
  3. 获取路径并加载图像.

活动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);
Run Code Online (Sandbox Code Playgroud)

使用此方法创建文件:

   //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();
}
Run Code Online (Sandbox Code Playgroud)

活动2:

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

//loads the file
File file = new File(filePath);
Run Code Online (Sandbox Code Playgroud)

最后加载图片:

Picasso.with(context).load(file).fit().centerCrop().into(imageView);
Run Code Online (Sandbox Code Playgroud)

要么:

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)