如何在Android中的活动之间发送大字节数组? [英] How to send large byte arrays between activities in Android?

查看:103
本文介绍了如何在Android中的活动之间发送大字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 byte [] data Activity1 发送到 Activity2 ,以便在jpg文件中写入数据(FileOutputStream.write(data))。我的最终.jpg文件可能超过1mb。

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.

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

}

在活动2中:

Bundle extras = getIntent().getExtras();
data = extras.getByteArray("photo");

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


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

  1. ImageSizeMyApp﹕ 446367 (this size sends to the next activity, and everything is good)

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

ImageSizeMyApp﹕ 577368 (this size closes my camera, and does not send to the next activity)

因此,500kb是Intent的限制维度。有没有其他稳定的方法可以在活动之间发送大于500kb的 byte []

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!

更新:

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

Could I make another class to store that byte[] array? Or is it better to use a static variable?

推荐答案


  1. 创建一个临时文件。

  2. 通过其他活动的文件路径。

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

使用此方法创建文件:

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

活动2:

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

//loads the file
File file = new File(filePath);

最后加载图片:

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

或者:

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

这篇关于如何在Android中的活动之间发送大字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆