2015-12-03 69 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

相关问题