2012-03-21 37 views
15

我正在學習如何拍攝照片並將其保存到文件中。getoutputmediafileuri方法不可訪問?

據對android developers website教程提供,用於

getoutputmediafileuri()的方法,但是,當我試圖用這個方法,我發現它

無法訪問或不確定的,我的意思是月食強調這個方法與紅線。我不知道

如何解決這個錯誤。

請找到該代碼

public class SaveCameraImageDemoActivity extends Activity { 
/** Called when the activity is first created. */ 

Button btn01; 
private Uri fileURI; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btn01 = (Button) findViewById(R.id.btn01); 
    btn01.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent intenet = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      fileURI = getoutputmediafileuri(); 
      //intenet.putExtra("output", uri.getPath()); 
      startActivityForResult(intenet,0); 
     } 
    }); 
} 

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

    } 
} 

回答

21

下面有沒有內在的方法getoutputmediafileuri()在android系統。 這是一種自定義方法,用於獲取文件URI以將捕獲的圖像存儲在特定目錄中。你必須爲它定義和放置邏輯。而不是使用此代碼,

編輯:

btn01.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     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); 
    } 
}); 

現在,這將存儲您的相機在MyImages目錄中的SD卡與image_001.jpg名拍攝的圖像。

+0

這也適用於視頻捕獲....並在文件中他們不提及它 – 2013-08-23 16:39:29

+0

如何找到Android默認圖像存儲路徑? – 2015-04-03 05:13:28

2

現在the document已添加的代碼。只需粘貼在你的Class的末尾,它對我來說很好。代碼見下文。

public static final int MEDIA_TYPE_IMAGE = 1; 
public static final int MEDIA_TYPE_VIDEO = 2; 

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

/** Create a File for saving an image or video */ 
private static File getOutputMediaFile(int type){ 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), "MyCameraApp"); 
    // This location works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 

    // Create the storage directory if it does not exist 
    if (! mediaStorageDir.exists()){ 
     if (! mediaStorageDir.mkdirs()){ 
      Log.d("MyCameraApp", "failed to create directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").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; 
} 
+0

你能告訴我一個例子如何在意圖添加上面的代碼? – Moudiz 2015-09-18 06:52:15