编程安卓方面,我是新手。我使用两种方式在活动之间传输图像,即使用 意图 或创建文件,但传输图像并在第二个活动的 imageview 中显示的大约需要 3 或 4 秒。有什么方法可以比这更快地传输,因为许多应用程序(例如whatsapp(正在以更快的速度传输。我的代码如下。任何帮助将不胜感激。
代码 在第一个活动中:
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.stopPreview();
Intent myintent = new Intent(CameraSetter.this,CameraPhotoViewer.class);
Bitmap bitmap_image = BitmapFactory.decodeByteArray(data, 0, data.length);
String fileName = "myImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap_image.compress(Bitmap.CompressFormat.JPEG, 50, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
startActivity(myintent);
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
}
};
其次:
Bitmap bitmap_image = BitmapFactory.decodeStream(getApplicationContext().openFileInput("myImage"));
imageview.setImageBitmap(bitmap_image);
它正在工作,但我想要更快的方式有什么想法吗?
还尝试将图像保存到内部存储,但也花费了太多时间。
代码是:
在第一个活动中:
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.stopPreview();
Intent myintent = new Intent(CameraSetter.this, CameraPhotoViewer.class);
Bitmap bitmap_image = BitmapFactory.decodeByteArray(data, 0, data.length);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath = new File(directory, "profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmap_image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
startActivity(myintent);
System.out.print(directory.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
在第二个活动中:
imageview = (ImageView) findViewById(R.id.imview_camera_setter);
framelayout = (FrameLayout) findViewById(R.id.frame_layout_viewer);
try {
File f=new File(internalpath, "profile.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
imageview.setImageBitmap(Bitmap.createScaledBitmap(b, 300, 300, false));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
仍然需要太多时间
您可以使用单例类来存储位图,而不是执行写入/读取磁盘(这很慢(。只是,使用后请注意清除位图。
从活动 A -> BitmapSingleton.getInstance((.setBitmap(bitmap(;
从活动 B -> BitmapSingleton.getInstance((.getBitmap(bitmap(;
使用它后,您应该在活动 B 中执行 BitmapSingleton.getInstance((.setBitmap(null(。
public class BitmapSingleton {
private Bitmap image = null;
private static final BitmapSingleton ourInstance = new BitmapSingleton();
public static BitmapSingleton getInstance() {
return ourInstance;
}
private BitmapSingleton() {
}
public Bitmap getBitmap() {
return image;
}
public void setBitmap(Bitmap image) {
this.image = image;
} }
你可以做的是获取一个存储文件路径的静态变量。然后,您可以在应用程序中的任何位置访问它。