我正在开发一个应用程序,它将绘制的画布作为jpeg图像存储在SD卡中。 问题是当我试图查看保存的图像时,它加载的时间比其他图像要多,我希望保存的图像在正常时间内像其他图像一样被查看 我保存图片的代码是:
View content = drawView;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
Bitmap bitmap = content.getDrawingCache();
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
String file_name="Imatge"+System.currentTimeMillis()+".jpg";
File file = new File(path,file_name);
FileOutputStream ostream;
try {
ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG,50, ostream);
ostream.flush();
ostream.close();
Toast.makeText(getApplicationContext(), " :) Image saved in "+ path+"/"+file_name, 5000).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString()+"error", 5000).show();
}
}
先谢谢,请帮帮我!!!!
答案 0 :(得分:0)
您的代码将图像保存为PNG文件(即使您为其指定了文件扩展名.jpg)。 PNG文件是无损的,因此比JPEG文件大得多。
要解决此问题,请将其另存为JPEG格式:
ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 50, ostream);
答案 1 :(得分:0)
使用如下所示
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
FileOutputStream out = null;
Log.e("h",""+mview.getMeasuredHeight()+" "+mview.getMeasuredWidth());
try {
out = new FileOutputStream(new File(extStorageDirectory + "/myAwesomeDrawing.jpg"));
mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
其中 mBitmap 是您在 Drawview 类中使用的Gloabal变量,因此使用此必须跳过此行menas在此行注释
/*View content = drawView;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
Bitmap bitmap = content.getDrawingCache();
*/
可能有效吗
答案 2 :(得分:0)
我自己找到解决方案,解决方案是通过媒体商店存储图像,这是我的代码正常工作
View content = drawView;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
Bitmap bitmap;
bitmap = content.getDrawingCache();
String file_name="MyImage"+System.currentTimeMillis()+".jpg";
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE,"MyImage");
values.put(Images.Media.DESCRIPTION,"Advanced Practice");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri url = null;
try
{
url = getContentResolver().insert(MediaStore.Images.Thumbnails.getContentUri("external"), values);
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap,"MyImage","Advanced Practice");
Toast.makeText(getApplicationContext(), " :) Image saved in /sdcard/DCIM/Camera/"+file_name, 5000).show();
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString()+"error", 5000).show();
}