2

Android: How to get an image from gallery if image size less than 1MB ??
+++My Code:

Bitmap bitmap;

private void onBrowse() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, 111);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 111 && resultCode == RESULT_OK && data != null){
            Uri path = data.getData();
            File file = new File(path.getPath());
            long size = file.length();
            Log.i("size=", size + "");
            if(size > 1048576){ // 1MB
                Toast.makeText(this, "Image must less than 1MB.", Toast.LENGTH_SHORT).show();
                return;
            }
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), path);
                ivImage.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

+++Output: I/size=: 0

I selected image 3.2MB, why did output 0?
I want to show an image in ivImage if file size of selected image less than 1MB.
Please help!!!

Dj Zenya
  • 21
  • 2

0 Answers0