1

I'm taking screenshot of my Layout with this code:

View u = findViewById(R.id.mainL);
u.setDrawingCacheEnabled(true);                                                
LinearLayout z = (LinearLayout) findViewById(R.id.mainL);
int totalHeight = z.getHeight();
int totalWidth = z.getWidth();
u.layout(0, 0, totalWidth, totalHeight);    
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());             
u.setDrawingCacheEnabled(false);

obviously I'm not doing anything on quality but the result bitmap (that is a jpeg image) has a very bad quality.

what am I doing wrong? I thing I should change it to a PNG Image somehow for better quality but I don't know how. thanks.

Edit:

for those who ask where am I saving the bitmap I have to say that I'm not saving it at all. I just share it with this code:

        String pathofBmp = MediaStore.Images.Media.insertImage(getContentResolver(), b , "title", null);
        Uri bmpUri = Uri.parse(pathofBmp);
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/jpeg");
        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(shareIntent, "Share"));
SaDeGH_F
  • 471
  • 1
  • 3
  • 14
  • "what am I doing wrong?" -- if nothing else, you are not showing the code where you are saving the JPEG file. "I thing I should change it to a PNG Image somehow for better quality but I don't know how" -- since you have not supplied the code where you are saving the JPEG file, we have no way of suggesting how you might change that code to save a PNG file instead. – CommonsWare Apr 02 '14 at 15:47

2 Answers2

3

I have to say that I'm not saving it at all

Yes, you are. Otherwise, you would not have pathofBmp. Now, in your case, you are delegating everything about saving the bitmap to the MediaStore. I have never used insertImage() and I have no idea what it does in terms of file formats, quality percentage, etc.

If you want more control over saving the image, use compress() on Bitmap to write it to a file yourself.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • by save I meant that the image won't be usable through Gallery outside the app. But Yes, I have to use that command. thanks. – SaDeGH_F Apr 03 '14 at 06:39
0

please try this:

View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, getString(R.string.free_tiket)+".jpg");
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(myPath);
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
    MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I think problem is compress 100 is fix your problem.

reference

Community
  • 1
  • 1
CompEng
  • 7,161
  • 16
  • 68
  • 122