我需要将byte[] data
从Activity1
发送到Activity2
,以便将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");
}
在Activity2中:
Bundle extras = getIntent().getExtras();
data = extras.getByteArray("photo");
我使用Log.w("ImageSizeMyApp", String.valueOf(data.length));
来获取此信息:
ImageSizeMyApp:446367(此大小发送到下一个活动,一切都很好)
ImageSizeMyApp:577368(此尺寸关闭我的相机,不会发送到下一个活动)
所以500kb是Intent的限制维度。还有其他稳定的方法可以在活动之间发送大于500kb的byte[]
吗?
欢迎任何参考或建议。提前谢谢!
更新
我可以创建另一个类来存储byte []数组吗?或者使用静态变量更好?
答案 0 :(得分:5)
活动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);
答案 1 :(得分:3)
我遇到了同样的问题。简而言之:继承Application
(或创建帮助单例),使用它(<application android:name=".App" .../>
中的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;
}
}
相机活动保存数据:
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();
}
};
父级活动读取数据:
@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);
}
}
答案 2 :(得分:0)
以下是步骤:
1)在sqlite中创建包含byte和id列的表 2)从活动1中保存此字节及其ID(您将给出) 3)转到活动2(不要忘记发送参数ID) 4)在Activity 2中,使用id从intent和query数据库中获取此参数,以获取字节。
我希望这会有所帮助。