Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top