I am writing a simple app that takes a photo, video or choose an existing photo/video.
I am writing the code for the first two options, but when I run it, it fails to create the directory for some reason. (I have correct permission in the manifest: ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE) Anyone have an idea why this code fails to create the directory (Log.e(TAG,"Failer to create directory");) The file should be saved in storage/sdcard/Pictures/nameOfTheApp... but instead when I log it: log: D/AppCompatActivity﹕ /storage/emulated/0/Pictures/nameOfTheApp its in a different place. this is the my code here:
public class MainActivity extends AppCompatActivity implements ActionBar.TabListener {
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;
// media path as Uri (Uniform resource unifier) unify system resorses
protected Uri mMediaUri;
protected DialogInterface.OnClickListener mDialogListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0: //Take picture
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMediaUri = getOutputMediaFileUrl(MEDIA_TYPE_IMAGE);
if(mMediaUri == null){
// display an error
Toast.makeText(MainActivity.this,getString(R.string.error_external_storage),Toast.LENGTH_LONG).show();
}
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,mMediaUri);
startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
break;
case 1:
break; //Take video
case 2:
break; //Choose picture
case 3:
break; //Choose video
}
}
private Uri getOutputMediaFileUrl(int mediaType) {
if(isExternalStoreageAvailable()){
//get URI
// 1. Get the external storage directory
String appName = MainActivity.this.getString(R.string.app_name);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
appName);
// 2. Create our subdirectory
if(! mediaStorageDir.exists()){
//creates directory. IF fails returns false
if(! mediaStorageDir.mkdirs()){
Log.e(TAG,"Failer to create directory");
return null;
};
}
// 3. Create a file name
// 4. Create the file
File mediaFile;
Date now = new Date();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.UK).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;
}
// 5. Return the file's uri
Log.d(TAG,"File: " + Uri.fromFile(mediaFile));
return Uri.fromFile(mediaFile);
}else{
return null;
}
}
private boolean isExternalStoreageAvailable(){
String state = Environment.getExternalStorageState();
if(state.equals(Environment.MEDIA_MOUNTED)){
return true;
}else{
return false;
}
}
};
Log:
05-29 10:30:02.487 23529-23529/koemdzhiev.com.blinkmessage I/art﹕ Late- enabling -Xcheck:jni
05-29 10:30:03.071 23529-23529/koemdzhiev.com.blinkmessage I/AppCompatActivity﹕ MariyaM
05-29 10:30:03.121 23529-23574/koemdzhiev.com.blinkmessage D/OpenGLRenderer﹕ Render dirty regions requested: true
05-29 10:30:03.133 23529-23529/koemdzhiev.com.blinkmessage D/Atlas﹕ Validating map...
05-29 10:30:03.213 23529-23574/koemdzhiev.com.blinkmessage I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1.C2.05.00.00.046.002_msm8974_LA.BF.1.1.1.C2__release_A U ()
OpenGL ES Shader Compiler Version: E031.25.03.00
Build Date: 01/06/15 Tue
Local Branch: mybranch6793908
Remote Branch: quic/LA.BF.1.1.1.c2
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1.C2.05.00.00.046.002 + NOTHING
05-29 10:30:03.216 23529-23574/koemdzhiev.com.blinkmessage I/OpenGLRenderer﹕ Initialized EGL, version 1.4
05-29 10:30:03.238 23529-23574/koemdzhiev.com.blinkmessage D/OpenGLRenderer﹕ Enabling debug mode 0
05-29 10:30:03.391 23529-23529/koemdzhiev.com.blinkmessage I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@37271f15 time:14614103
05-29 10:30:06.647 23529-23539/koemdzhiev.com.blinkmessage W/art﹕ Suspending all threads took: 40.361ms
05-29 10:30:08.772 23529-23529/koemdzhiev.com.blinkmessage E/AppCompatActivity﹕ Failer to create directory
05-29 10:30:09.069 23529-23574/koemdzhiev.com.blinkmessage V/RenderScript﹕ Application requested CPU execution
05-29 10:30:09.248 23529-23574/koemdzhiev.com.blinkmessage V/RenderScript﹕ 0xafa7d000 Launching thread(s), CPUs 4
05-29 10:30:21.531 23529-23529/koemdzhiev.com.blinkmessage I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@37271f15 time:14632243
05-29 10:32:05.225 23529-23529/koemdzhiev.com.blinkmessage I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@37271f15 time:14735937
05-29 10:32:39.136 23529-23529/koemdzhiev.com.blinkmessage E/AppCompatActivity﹕ Failer to create directory
05-29 10:41:31.324 23529-23539/koemdzhiev.com.blinkmessage W/art﹕ Suspending all threads took: 5.421ms
将ANDROID.PERMISSION
小写。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.