我正在制作应用程序进行练习。在应用程序中,用户可以拍摄照片,照片将保留在外部存储器中。在下面有一些我的应用程序的代码。
public class MainActivity extends AppCompatActivity implements ActionBar.TabListener {
public static final String TAG = MainActivity.class.getSimpleName();
public static final int TAKE_PHOTO_REQUEST = 0;
public static final int TAKE_VIDEO_REQUEST = 1;
public static final int PICK_PHOTO_REQUEST = 2;
public static final int PICK_VIDEO_REQUEST = 3;
public static final int MEDIA_TYPE_IMAGE = 4;
public static final int MEDIA_TYPE_VIDEO = 5;
private Uri mMediaUri;
private DialogInterface.OnClickListener mDialogListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMediaUri = getOutPutMediaFileUri(MEDIA_TYPE_IMAGE);
if (mMediaUri == null) {
//display an error
Toast.makeText(MainActivity.this, R.string.error_external_storage, Toast.LENGTH_LONG).show();
} else {
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
}
break;
case 1:
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
mMediaUri = getOutPutMediaFileUri(MEDIA_TYPE_VIDEO);
if (mMediaUri == null) {
//display an error
Toast.makeText(MainActivity.this, R.string.error_external_storage, Toast.LENGTH_LONG).show();
} else {
videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
videoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
videoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); //0 for low quality
startActivityForResult(videoIntent, TAKE_VIDEO_REQUEST);
}
break;
case 2:
break;
case 3:
break;
}
}
private Uri getOutPutMediaFileUri(int mediaType) {
if (isExternalStorageAvailable()) {
//Get the external storage directory
String appName = MainActivity.this.getString(R.string.app_name);
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), appName);
//Create our subdirectory
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d(TAG, "failed to create directory");
return null;
}
}
//Create file name
//Create the file
File mediaFile;
Date now = new Date();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(now);
String path = mediaStorageDir.getPath() + File.separator;
if (mediaType == MEDIA_TYPE_IMAGE) {
mediaFile = new File(path + "IMG_" + timestamp + ".jpg");
} else if (mediaType == MEDIA_TYPE_VIDEO) {
mediaFile = new File(path + "VID_" + timestamp + ".mp4");
} else {
return null;
}
Log.d(TAG, "File: " + Uri.fromFile(mediaFile));
//Return the file's uri
return Uri.fromFile(mediaFile);
} else {
return null;
}
}
private boolean isExternalStorageAvailable() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
};
我在清单文件中添加了这两行
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera2" android:required="true"/>
但是当我运行应用程序并选择拍摄照片时,会出现一个吐司,并说访问外部存储器时出现问题。并且在日志中表示无法创建目录。[注意:吐司是仅在mMediaUri为空时才显示]
我还在我的模拟器中定义了sdcard
为什么mMediaUri返回null; 那么,我的代码出了什么问题。我的代码中没有看到任何错误。 请解决这个问题。