2012-03-21 18 views
15

私は写真を撮ってそのパスをファイルに保存する方法を学んでいます。私は、そのメソッドを使用しようとしたときgetoutputmediafileuriメソッドにアクセスできませんか?

android developers website上のチュートリアルの提供、

getoutputmediafileuri()が使用されている方法によると、しかし、私はそれがアクセスまたは未定義

ないことが判明し、私はこのことを強調日食意味します赤い線での方法。私は知らない

このエラーを修正する方法。

アンドロイドには作り付けの方法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

を見つけてください。 キャプチャされたイメージを特定のディレクトリに保存するためにファイルURIを取得するために書き込むカスタムメソッドです。それを定義してロジックを置く必要があります。代わりに、その使用が、このコードは、

EDIT:

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

は今、これはあなたのカメラがimage_001.jpg名前でSDカードにMyImagesディレクトリで撮影した画像を保存します。

+0

これはビデオキャプチャのためにも機能します....ドキュメントには言及しません –

+0

Androidのデフォルトイメージストレージパスを見つけるにはどうすればいいですか? –

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

関連する問題