在 activity 之间传输图像的最快方式
fastest way to transfer images between activity
我是编程新手android。我正在使用两种方式在 activity 之间传输图像,即通过使用 intent 或通过创建文件,但传输图像并在第二个 activity 的图像视图中显示大约需要 3 或 4 秒。有什么方法可以让我的传输速度更快,因为许多应用程序(例如 whatsapp)的传输速度更快。我的代码如下。任何帮助将不胜感激。
首先输入代码 activity:
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);
它正在运行,但我想要更快的方式有什么想法吗?
也尝试过将图像保存到内部存储器,但也花费了太多时间。
代码是:
第一个activity:
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();
}
}
}
};
第二个 Activity:
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();
}
仍然需要太多时间
您可以使用单例 class 来存储您的位图,而不是使用 write/read 磁盘(这很慢)。只是,使用后注意清除位图。
来自activity A -> BitmapSingleton.getInstance().setBitmap(位图);
来自activity B -> BitmapSingleton.getInstance().getBitmap(位图);
使用后,你应该做BitmapSingleton.getInstance().setBitmap(null);在 activity B.
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;
} }
你可以做的是获取一个存储文件路径的静态变量。然后您可以在应用程序的任何位置访问它。
我是编程新手android。我正在使用两种方式在 activity 之间传输图像,即通过使用 intent 或通过创建文件,但传输图像并在第二个 activity 的图像视图中显示大约需要 3 或 4 秒。有什么方法可以让我的传输速度更快,因为许多应用程序(例如 whatsapp)的传输速度更快。我的代码如下。任何帮助将不胜感激。
首先输入代码 activity:
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);
它正在运行,但我想要更快的方式有什么想法吗?
也尝试过将图像保存到内部存储器,但也花费了太多时间。
代码是:
第一个activity:
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();
}
}
}
};
第二个 Activity:
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();
}
仍然需要太多时间
您可以使用单例 class 来存储您的位图,而不是使用 write/read 磁盘(这很慢)。只是,使用后注意清除位图。
来自activity A -> BitmapSingleton.getInstance().setBitmap(位图);
来自activity B -> BitmapSingleton.getInstance().getBitmap(位图);
使用后,你应该做BitmapSingleton.getInstance().setBitmap(null);在 activity B.
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;
} }
你可以做的是获取一个存储文件路径的静态变量。然后您可以在应用程序的任何位置访问它。