2015-12-03 131 views
1

我正在創建一個應用程序,我希望用戶選擇將文件保存在哪個文件夾中。我首先要查看是否可以找出如何將圖像保存在另一個文件夾中默認的文件夾,但我不斷得到一個錯誤,不知道爲什麼。從相機存儲圖像在新文件夾中android

這裏是我必須啓動相機意圖和獲取文件目錄名

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_smart_wardrobe); 
    camera = (Button)findViewById(R.id.cameraBtn); 
    camera.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      file = getOutputMediaFileUri(); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, file); 
      startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
     } 
    }); 

} 

下面的代碼是爲uriFileDirectory

private static Uri getOutputMediaFileUri(){ 
    return Uri.fromFile(getOutputMediaFile()); 
} 

private static File getOutputMediaFile(){ 
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), "MyCameraApp"); 

    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; 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
       "IMG_"+ timeStamp + ".jpg"); 


    return mediaFile; 
} 

下面的代碼是我的onActivityResult方法

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
     if (resultCode == RESULT_OK) { 
      // Image captured and saved to fileUri specified in the Intent 
      Toast.makeText(this, "Image saved to:\n" + 
        data.getData(), Toast.LENGTH_LONG).show(); 
     } else if (resultCode == RESULT_CANCELED) { 
      // User cancelled the image capture 
     } else { 
      Toast.makeText(this, "There is someting wrong with your code", Toast.LENGTH_LONG).show(); 
     } 
    } 
    if (requestCode == SELECT_PICTURE){ 
     if (resultCode == RESULT_OK) { 
       images = data.getData(); 
       selectedImagePath = getPath(images); 
     } 
    } 
} 

有人可以幫助修復我的代碼嗎?

+0

你得到了什麼錯誤? – Jas

+0

請張貼您的'日誌貓'。您還需要在清單中添加'<使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/>'權限。 –

+0

你有什麼錯誤? –

回答

2

首先檢查您在Manifest的許可。

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

onActivityResult方法檢查就是它返回一個文件路徑或沒有。在此方法中添加行

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
     if (resultCode == RESULT_OK) { 
      // Image captured and saved to fileUri specified in the Intent 
      Toast.makeText(this, "Image saved to:\n" + 
        file, Toast.LENGTH_LONG).show(); 
     } else if (resultCode == RESULT_CANCELED) { 
      // User cancelled the image capture 
     } else { 
      Toast.makeText(this, "There is someting wrong with your code", Toast.LENGTH_LONG).show(); 
     } 
    } 

問題出現在您的祝詞中。在Camera Intent中,我們將意圖設置爲null,並且您試圖從那裏獲取數據。你必須使用Uri。

+0

我添加了權限和額外的代碼行,仍然是相同的錯誤 – Ashley

+0

@Ashley張貼您的錯誤也在您的問題 – curiousMind

+0

@Ashley你在嗎? – curiousMind

相關問題