2017-07-06 63 views
0

我正在嘗試編寫啓動相機應用程序,保存圖像,然後顯示捕獲圖像的位圖的代碼。(Android)相機捕捉不保存,無法加載圖像的位圖

這是相關的代碼,這是改編自https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media

public static final int PICTURE_REQUEST_CODE = 1034; 
    public static final String FILE_BEGIN = "note"; 
    private int note_id = 4; 
    public static final String FILE_END = ".jpg"; 
    private static final String APP_TAG = "School_App"; 

    private void takePicture() { 
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    i.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(FILE_BEGIN + note_id + FILE_END)); 
    if (i.resolveActivity(getPackageManager()) != null){ 
     startActivityForResult(i, PICTURE_REQUEST_CODE); 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == PICTURE_REQUEST_CODE) { 
     if (resultCode == RESULT_OK) { 
      Uri takenPhotoUri = getPhotoFileUri(FILE_BEGIN + note_id + FILE_END); 
      Log.d("NotesDetail", takenPhotoUri.toString()); 
      // by this point we have the camera photo on disk 
      Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath()); 
      // RESIZE BITMAP, see section below 
      // Load the taken image into a preview 
      ImageView ivPreview = (ImageView) findViewById(R.id.iv_notes_picture); 
      ivPreview.setImageBitmap(takenImage); 
     } else { // Result was a failure 
      Toast.makeText(this, "Picture wasn't taken!", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

private Uri getPhotoFileUri(String fileName) { 
    if(isSpaceAvailable()){ 
     File mediaStorageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG); 

     if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){ 
      Log.d(APP_TAG, "Failed to make directory"); 
     } 

     File file = new File(mediaStorageDir.getPath() + File.separator + fileName); 

     Uri uri = FileProvider.getUriForFile(NotesDetail.this, "com.kfode.schoolapp.fileprovider", file); 
     Log.d("getPhotoFileUri", uri.toString()); 
     return uri; 
    } 
    return null; 
} 

private boolean isSpaceAvailable() { 
    String state = Environment.getExternalStorageState(); 
    return state.equals(Environment.MEDIA_MOUNTED); 
} 

我也有以下權限設置:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

在清單的應用標籤...

<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.kfode.schoolapp.fileprovider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/file_paths" 
      /> 
    </provider> 

而且在file_paths.xml中

<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
<external-files-path name="images" 
    path="Pictures"/> 

當我運行應用程序,我得到以下日誌消息和異常:

D/getPhotoFileUri: content://com.kfode.schoolapp.fileprovider/images/School_App/note4.jpg 
D/NotesDetail: content://com.kfode.schoolapp.fileprovider/images/School_App/note4.jpg 
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /images/School_App/note4.jpg: open failed: ENOENT (No such file or directory) 

不知道這到底是怎麼回事。請幫助?

回答

0
Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath()); 

這是行不通的。 A Uri不是File

重構getPhotoFileUri()爲兩種方法:

  • getPhotoFile()負責產生File
  • getPhotoFileUri(),其取得上述FileProviderUriFile

然後,使用圖像加載庫(滑翔,畢加索等)從返回的文件填充您的。這不僅會爲你處理所有的BitmapFactory東西,還會在後臺線程中完成這項工作,所以你不會像你當前的代碼那樣凍結你的UI。

+0

非常感謝您的幫助! :) – user2991144