2015-06-23 95 views
0

我運行一個新的意向訪問攝像機在Android中無法識別,但我得到這個錯誤:getOutputMediaFileUri方法在Android攝像頭意圖

getOutputMediaFileUri method not recognized

的代碼如下

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 


startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

回答

0

您的代碼似乎來自this Android相機教程。

它說:

The getOutputMediaFileUri() method in this example refers to the sample code shown in Saving Media Files .

而且在該鏈接中,我們發現代碼:

/** Create a file Uri for saving an image or video */ 
private static Uri getOutputMediaFileUri(int type){ 
     return Uri.fromFile(getOutputMediaFile(type)); 
} 

所以上面的代碼添加到您的代碼,你要善於去。

3
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages"); 
     imagesFolder.mkdirs(); // <---- 
     File image = new File(imagesFolder, "image_001.jpg"); 
     Uri uriSavedImage = Uri.fromFile(image); 
     imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
     startActivityForResult(imageIntent,0); 

現在這會將您的相機拍攝的圖像存儲在sdcard中的MyImages目錄中,並帶有image_001.jpg名稱。

0

您還沒有正確閱讀本文。您必須將以下方法添加到您的MainActivity.java

/** 
* Creating file uri to store image/video 
*/ 
public Uri getOutputMediaFileUri(int type) { 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 

/* 
* returning image/video 
*/ 
private static File getOutputMediaFile(int type) { 

    // External sdcard location 
    File mediaStorageDir = new File(
      Environment 
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
      IMAGE_DIRECTORY_NAME); 

    // Create the storage directory if it does not exist 
    if (!mediaStorageDir.exists()) { 
     if (!mediaStorageDir.mkdirs()) { 
      Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " 
        + IMAGE_DIRECTORY_NAME + " directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
      Locale.getDefault()).format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator 
       + "IMG_" + timeStamp + ".jpg"); 
    } else if (type == MEDIA_TYPE_VIDEO) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator 
       + "VID_" + timeStamp + ".mp4"); 
    } else { 
     return null; 
    } 

    return mediaFile; 
} 
相關問題