Improve image quality while using getDrawingCache - android

你说的曾经没有我的故事 提交于 2019-12-19 04:05:07

问题


I am capturing my layout view using getDrawingCache() method and creating image from it. The code works fine and image gets generated, but the problem here is, the generated image quality is very low. I want the resolution of generated image to be high so that whenever i set it to ImageView it don't get stretched.

Here is the code which I am using :

RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout); layout.setDrawingCacheEnabled(true); Bitmap bmp = layout.getDrawingCache(); File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + System.currentTimeMillis()+".jpg"); try { FileOutputStream out = new FileOutputStream(f); bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); }

P.S. - I also tried using layout.buildDrawingCache(true); and layout.setDrawingCacheQuality(RelativeLayout.DRAWING_CACHE_QUALITY_HIGH); before calling layout.getDrawingCache(); but no changes in quality were found.

Can anyone please help me out on this, how can I improve quality of the image being generated? Thanks in advance.


回答1:


After Searching for very long and trying out different solutions, Finally I got the captured Image quality better than it was earlier.

I changed my code as :

RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache(true);
final Bitmap bmp = Bitmap.createBitmap(layout.getDrawingCache());
layout.setDrawingCacheEnabled(false);
Canvas c= new Canvas(bmp);


File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/" + System.currentTimeMillis()+".jpg");
try {

    FileOutputStream out = new FileOutputStream(f);
    c.drawBitmap(bmp, 0, 0, null);
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);

    out.flush();
    out.close();
} catch (IOException e) {
    e.printStackTrace();
}

This is what helped me. Hope it'll help others too.



来源:https://stackoverflow.com/questions/22838387/improve-image-quality-while-using-getdrawingcache-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!