如何使用BitmapFactory在AndroidView中显示来自SD卡的JPG文件--Android Studio

时间:2017-08-28 11:56:18

标签: android bitmap

我可以使用一些帮助。这是我的第一个android项目。我想用相机拍照,然后在ImageView中显示。我可以拍照并将其保存在SD卡上的文件夹中(文件路径始终相同),但我无法在ImageView中显示它。 (文件在那里,因为我使用相同的路径通过email.intent通过电子邮件客户端发送它并且它有效。)

显示图片的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    final File imgFile = new  File("/storage/emulated/0/camera_app/cam_image.jpg");
    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        ImageView kamera3= (ImageView) findViewById(R.id.kamera2);
        kamera3.setImageBitmap(myBitmap);
     }

我也试过了:

Bitmap myBitmap = BitmapFactory.decodeFile("/storage/emulated/0/camera_app/cam_image.jpg");
ImageView kamera3= (ImageView) findViewById(R.id.kamera2);
kamera3.setImageBitmap(myBitmap);

2 个答案:

答案 0 :(得分:0)

uri传递给此功能

public static Bitmap createBitmap(String photoPath) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
    return bitmap;
}

然后将其设置为ImageView

image_view.setImageBitmap(Constants.createBitmap("uri"));

答案 1 :(得分:0)

if (ContextCompat.checkSelfPermission(prva.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(prva.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

        {    ActivityCompat.requestPermissions(prva.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSION_WRITE_EXTERNAL_STORAGE);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }