0

I have a class where i take the screenshot of the layout and store it as a JPEG file....the problem i am having is the image size varies with the screen resolution.... for eg. if i use medium resolution phones the image size is around 50-60kb but for high resolution it goes upto 1.5mbs....so is there any way i can keep the image size constant independent of screen size or resolution???

public class abc extends Activity {
    View content;
    String fileName, fname;
    static File file;
    static String RECE;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rece);
        ImageView iv = (ImageView) findViewById(R.id.Ivsignature);
        iv.setImageBitmap(Signature.sign);
        Initialize();

        content = findViewById(R.id.Rlrece);
        ViewTreeObserver vto = content.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            public void onGlobalLayout() {
                content.getViewTreeObserver()
                        .removeGlobalOnLayoutListener(this);
                getScreen();
            }
        });

    }

    private void getScreen() {
        View view = content;
        View v = view.getRootView();
        v.setDrawingCacheEnabled(true);
        Bitmap b = v.getDrawingCache();

        String myPath = Environment.getExternalStorageDirectory() + "/Rece";
        File myDir = new File(myPath);
        try {
            myDir.mkdirs();
        } catch (Exception e) {
        }

        fname = fileName + ".jpg";
        file = new File(myDir, fname);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            b.compress(CompressFormat.JPEG, 100, fos);
            // Toast.makeText(getApplicationContext(), fname + "  saved",
            // Toast.LENGTH_LONG).show();
            Intent my = new Intent(getApplicationContext(),
                    DialogActivity.class);
            my.putExtra("Rsystrace", DialogActivity.Systrace);
            startActivityForResult(my, 1);
        } catch (Throwable ex) {
            Toast.makeText(getApplicationContext(),
                    "error: " + ex.getMessage(), Toast.LENGTH_LONG).show();

        }
    }
Adolf Dsilva
  • 13,092
  • 8
  • 34
  • 45

2 Answers2

0

When you do screenshot you'll ofcourse have screen size dependent image (the resolution of the image is different because screens are different).

You can compress or resize you bitmap as you want and after resize to the fixed size - the file size will be fixed.

PS. I suggest you to use PNG except of JPEG.

dilix
  • 3,761
  • 3
  • 31
  • 55
0

You can create a object BitmapFactory.Options() and set the property inSampleSize given a scale factor (you will need to calculate this). Than you can use like in BitmapFactory.decodeFile() to take a image at the size you want (based on inSampleSize).

-- EDIT --

Example using a image in a disk at "photoPath":

    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    BitmapFactory.decodeFile(photoPath, bmOptions);

    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    int scaleFactor = Math.min(newWidth/width, newHeigth/heigth);
    bmOptions.inSampleSize = scaleFactor;

    Bitmap bitmap = BitmapFactory.decodeFile(photoPath, bmOptions);

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
    FileOutputStream fos;
    fos = new FileOutputStream(photoPath);
    fos.write(bytes.toByteArray());
    fos.close();
Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32