2017-08-28 33 views
0

我可以使用一些帮助。这是我的第一个Android项目。我想用相机拍照,然后在ImageView中显示它。我可以拍照并将其保存在SD卡上的文件夹中(文件路径始终相同),但我无法在ImageView中显示它。 (文件是存在的,因为我使用的是相同的路径email.intent超过电子邮件客户端发送和它的作品。)如何使用BitmapFactory在ImageView中显示SD卡中的JPG文件 - Android Studio

代码用于显示图像:

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); 
+0

你检查运行许可? –

+0

粘贴您的完整代码,因为您没有在上面的onActivityResult方法中检查requestCode和resultCode。 –

+0

添加权限清单文件'<使用权限android:name =“android.permission.WRITE_EXTERNAL_STORAGE”/>' –

回答

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")); 
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. 
     } 
    } 
+0

这是失踪 – Tim

+0

我也添加私人静态最终诠释MY_PERMISSION_WRITE_EXTERNAL_STORAGE = 1; – Tim

相关问题