I am writing a simple activity to record and save audio, preferably to a folder within my application, but, for simplicity, to the SD card. The line of code that's giving me trouble is
String path = Environment.getExternalStorageDirectory().toString() + "/" + "tempAppFiles/";
String filename = "test"+".mp4";
recorder.setOutputFile(path + filename);
where recorder is an instance of MediaRecorder.
When I run the application, I get a permissions error that states
07-31 15:51:51.810: W/System.err(13670): java.io.FileNotFoundException: /mnt/sdcard/tempAppFiles/test.mp4 (Permission denied)
I looked this problem up and found that I needed to add several permission tags to my manifest, and I added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
to my manifest.
I am still getting the same permissions issue, and I can't find anyone with a similar problem.
Any ideas?
Hmm... The permissions seem to be correct, perhaps you've put them in the wrong place, they need to be children of the root note .
Next you can check, whether you can write the file by checking
boolean canIWrite = path.canWrite();
As you also get a FileNotFound exception you could try...
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
...instead of...
String path = Environment.getExternalStorageDirectory().toString();
If that is no help at all, there is still an official example of capturing audio - you should compare your code to:
http://developer.android.com/guide/topics/media/audio-capture.html
tempAppFiles does not set well with me. Seems like you should be writing to a directory that is under the package name of the app you are writing ...
Related
First and foremost, I'm testing this app using my Google Pixel XL (1st gen 32 GB version, runnning Android 9 API Level 28)
Basically, I'm trying to make a camera application based on Android Camera 2 API (I just downloaded the sample code from https://github.com/googlesamples/android-Camera2Basic). and I'm stuck because I'm trying to save the picture taken by the camera into "Pictures" folder inside "/storage/emulated/0/".
However, for some reason even though it seems like I'm able to do this, the folder and the picture file are not there when I checked them with ES File Explorer.
I've tried 3 methods so far:
Attempt #1:
mFile = new File(getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES), "newPicture.jpg");
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
mFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
mFile.mkdirs();
}
Attempt #2:
File filePath = new File(Environment.getExternalStorageDirectory().getPath(),"randomFolder");
if(!filePath.exists()){
filePath.mkdirs();
}
String fullPathName = filePath.getAbsolutePath() + "/" + "newPicture.jpg";
mFile = new File(fullPathName);
Attempt #3:
mFile = new File(fullPathName);
File filePath = new File(Environment.getExternalStorageDirectory() + "/Pictures/");
if (!filePath.exists()){
filePath.mkdirs();
}
mFile = new File(filePath,"newPicture.jpg");
mFile is the File path where "newPicture" will be saved to, basically it's supposed to be saved to "/storage/emulated/0/Pictures/newPicture.jpg"
Now, I've tried to use getExternalFilesDir() method so the picture will be saved into /storage/emulated/0/Android/data/com.***.***[the package name]/files/Pictures/newPicture.jpg" and in this case, IT TOTALLY WORKS! However I REALLY want to save the picture to /storage/emulated/0 directory because I want user to be able to access the data easily later on.
In order to illustrate this better, I've recorded the application running and then took a picture and saved it in a gif, uploaded it to imgur right here:
https://imgur.com/Szpc1HR
See that? The picture seems to be saved into "/storage/emulated/0/Pictures/newPicture.jpg" successfully but then when I went to ES File Explorer to check the picture, the folder "Pictures" was not created, nada.
I'm literally on my wits end right here haha, does anyone know a solution to this problem? Any advice will be greatly appreciated
P.S. Almost forgot to say, I've already added the permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
As someone said in this post:
how-can-i-access-storage-emulated-0-dcim-on-android-device
,
The "/storage/emulated/" folder does not really exist. It's what might be called a "symbolic link", or, in simpler terms, a reference to where the real data is stored. You'll need to find the actual physical location on your device where it is stored.
Maybe this link will help, Or this Can't create a directory on /storage/emulated/0 on emulator.
First off I thought this issue might be experienced thousands of times and already solved long time ago. However it was not the case, I red the following posts 1, 2, 3 and others but none of the helped.
So here is my case.
I am working on chat application that allows users to attach files, to make that happen I am developing file explorer but I keep running into NullPointerException. Here is my code.
String extState = Environment.getExternalStorageState();
if(extState.equals(Environment.MEDIA_MOUNTED)) {
File sd = Environment.getExternalStorageDirectory();
File[] sdDirList = sd.listFiles();
}
Here I am checking if external storage is mounted and it returns true. However it keeps throwing NullPointerException because sd.listFiles() return null.
in manifest I have the following permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I am testing on android 7.0
Do you request permissions for reading external storage?
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
Let set permission inside your phone. It worked for me
I tried to change the file name of a song by programmatically by using the code
File from = new File(filedirectory,currentName+type);
File to = new File(filedirectory,newName+type);
from.renameTo(to);
But after executing the code the current file is not show in the music list, also the new one is not showing in the list.
The file path and the file name are:
filedirectory: /storage/emulated/0/media/audio/music/
newName: aaaa.m4a
currentName: bbb.m4a
In the manifest I given all the necessary permission and am executing the code in Kitkat.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
After rename the file, I checked whether the file exists or not by pragmatically, the result is file not exists with the new name.
Have you ever tried to use "adb shell" and "cd /storage/emulated/0/media/audio/music/", then list file and check the files there? Whether the filename has been changed or not?
If the filename changed, then it is an issue of sync in the Music Player.
I have ever met issue that I changed the photo album, but cannot get the changes in my App by get system photo album through content provider. I have no idea how to solve it, and it disappears after phone restarted.
I'm working in a listActivity and I'm trying to show the children of a given directory. Here is what I've tried to get the children of a given directory :
File currentFile = new File(Environment.getExternalStorageDirectory(), "Pictures");
String[] children = currentFile.list();
In fact, "children" is null and I don't know why. When I ask the absolute path of "currentFile", the answer is "/storage/emulated/0/Pictures".
Thanks for your help!
Make sure the folder exists, and check if you have added the permission to your manifest.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and if you are running on Android M, you would need to request runtime permission
https://developer.android.com/preview/features/runtime-permissions.html#coding
Once again, I've come up against a question which has been asked and answered before but in my implementation it is still not working.
I'm calling getExternalFilesDir(null) right at the very start of my main activity's onCreate method. It returns null every time, whether I run it in an AVD or on my phone (Samsung Galaxy Plus).
Yes, I have the <uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" /> line in my AndroidManifest.xml and yes, I am checking the external storage state before I make the call and it is mounted.
Here are the first three lines inside my onCreate() method. Actually, it's just after the super.onCreate() and setContentView() calls.
String state = Environment.getExternalStorageState();
File extFiles = getExternalFilesDir(null);
File locFiles = getFilesDir();
So, once these three lines have executed, these are the values for the variables:
state == "mounted"
extFiles == null
locFiles == "/data/data/com.mypackage.name/files"
Would anyone have any ideas as to why this might be?
-----EDIT-----
So I've tried another approach; Rather than using getExternalFilesDir(null), I tried using File basePath = new File(Environment.getExternalStorageDirectory(), "myAppName");
This is not ideal and I know that the Android documentation says, and I agree with it, that you should rather use getExternalFilesDir(). Seeing as that's not working for me though I had to try something else. This time the function does return a valid File object so, after the above line, the path of basePath is /mnt/sdcard/myAppName. So far, so good. When I check with DDMS I can see that /mnt/sdcard exists but not /mnt/sdcard/myAppName. This is to be expected. So I call boolean result = basePath.mkdirs();
But this returns false and when I check on the file system I can confirm that the myAppName subfolder has not been created. When I create the folder manually through DDMS and put files in it, I can read those files from my application but I can't write anything in that folder.
Please help! I'm at my wit's end.
If this wasn't a typo when you posted your question, you'll probably hate yourself for this:
<uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" />
should be
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
This is from Android documentation:
Returns the path of the directory holding application files on external storage.
Returns null if external storage is not currently mounted so it could not ensure
the path exists; you will need to call this method again when it is available.
The other option is you can check if External storage is available:
String state = Environment.getExternalStorageState();
File filesDir;
// Make sure it's available
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
filesDir = getExternalFilesDir(null);
} else {
// Load another directory, probably local memory
filesDir = getFilesDir();
}
My issue was that I opened a FileOutputStream, then before I closed the FileOutputStream, I opened a FileInputStream to see what was already in the file.
I moved opening the FileInputStream to before the FileOutputStream is opened and that fixed my issue.
Delete a line
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
in AndroidManifest.xml.
Switch a xml editor to "Android Manifest Permissions" with "Permission" tab in eclipse, and add a
uses-permission "android.permission.WRITE_EXTERNAL_STORAGE"
with some clicks.
Then try running your application.
It seems eclipse (may depends on a defference of version or state or settings) can ignore some words described by direct xml in AndroidManifest.xml.
Thanks for an advise. You are right, my answer looked like to agree in small talk.