Android处理大图像
我让用户选择背景,用户可以从图库中选择自己的图片,或者从我随应用程序带来的列表中选择自己的图片。我将此图片保存到FileOutputStream。然而,我的应用程序变得非常慢,我使用以下函数Android处理大图像,android,bitmapfactory,Android,Bitmapfactory,我让用户选择背景,用户可以从图库中选择自己的图片,或者从我随应用程序带来的列表中选择自己的图片。我将此图片保存到FileOutputStream。然而,我的应用程序变得非常慢,我使用以下函数 //When user picks from gallery public void save(Bitmap bitmapImage) { bitmapImage = scaleBitmap(bitmapImage); FileOutputStream fileOutputStream =
//When user picks from gallery
public void save(Bitmap bitmapImage) {
bitmapImage = scaleBitmap(bitmapImage);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(createFile());
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//When user picks from list and I load from drawables
public void save(int resId) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = IMAGE_IN_SAMPLE_SIZE;
options.inScaled = false;
options.inJustDecodeBounds = false;
Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), resId, options);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(createFile());
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
如何使这些功能更快
我还收到以下信息:
06-15 12:02:58.884 22320-22320/com I/dalvikvm-heap: Grow heap (frag case) to 15.172MB for 2073616-byte allocation
06-15 12:03:00.716 22320-22320/com I/dalvikvm-heap: Grow heap (frag case) to 15.149MB for 2073616-byte allocation
06-15 12:03:03.129 22320-22320/com I/Choreographer: Skipped 145 frames! The application may be doing too much work on its main thread.
你不应该在主线程中做IO工作,也许你可以在一个线程中做,然后用hanlder更新你的ui
new Thread(new Runnable() {
@Override
public void run() {
// save your picture
handler.sendEmptyMessage(0);
}
}).start();
最好使用rxjava和rxAndroid
希望它能帮助你如果图像太大,在另一个线程上工作也没有帮助,它仍然会导致OutOfMemory 你应该尝试使用Glide库来做你的事情,因为他们以最好的方式来实现它 此链接用于Glide\Picasso分期付款和基本用途- 用于缩放-
考虑使用glide我想使用glide,但如何使用glide将图像保存到文件中?