Android:如果图像大小小于1MB,如何从图库中获取图像?

时间:2017-06-09 14:22:25

标签: android

Android:如果图像尺寸小于1MB,如何从图库中获取图像?
+++我的代码:

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();
            }
        }
    }

+++输出:I / size =:0

我选择图像3.2MB,为什么输出0?
如果所选图像的文件大小小于1MB,我想在ivImage中显示图像 请帮忙!!!

0 个答案:

没有答案