2017-08-28 35 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

相關問題