I have an MP3 in my sdcard and I need to use its path for my setdatasource command. I have tried mp.setDataSource("/mnt/media_rw/sdcard/mymusic/thebomb.mp3"); along with many variations but it still won't work. And I don't think it has to do with the rest of the code.
How would I accomplish this?
I guess you are using Emulator.So the path of the SDCard can be get through Environment.getExternalStorageDirectory().Moreover your sdcard must contain that file.So you have to push your file in the SD card.
Use this way:
Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+"/mymusic/thebomb.mp3");
musicPlayer.setDataSource(uri);
Add permission to your manifest.xml file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
try this like...Simple mediaplayer play mp3 from file path?.
Hope so it will help you.
mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/intro.mp3"));
mpintro.setLooping(true);
mpintro.start();
Use the below code:
musicPlayer.setDataSource("/sdcard/mymusic/thebomb.mp3");
Try with this -
mp.setDataSource(Environment.getExternalStorageDirectory()+"/mymusic/thebomb.mp3");
Try the following code and is working:
mp.setDataSource("/sdcard/mymusic/thebomb.mp3");
Related
I am trying to put an image in an imageView by code. I have some images in the Drawable directory but, with other images, I have to use the internal storage. In first case,
holder.picture.setImageDrawable(getResources().getDrawable(R.drawable.my_image));
works but, in the second case, Android give me this error:
BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Sensia_Sniffer/18_04_2018/18_56_18_photo.JPEG (No such file or directory)
and I don't know why. I am using this lines:
Drawable drawable = Drawable.createFromPath(my_image_path);
holder.picture.setImageDrawable(drawable);
and in AndroidManifest.xml I have this permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Could you help me with this?
Thank you very much!!!
You forgot to ask for runtime permissions for the requested permissions in manifest file.
Needed for Android 6+.
This is the previous post.
About mp3 player
And the picture below is my A.mp3 path I find in my phone.
`
mediaPlayer.setDataSource("/storage/sdcard1/A.mp3")
File file =new File(Environment.getExternalStorageDirectory(),"A.mp3");
mediaPlayer.setDataSource(file.getPath());
There are two paths above..According the picture,it should be the first one,but it does not work.
I push A.mp3 into the internal storage,and play is ok.
mediaPlayer.setDataSource("/system/A.mp3");
I finally find that the two files got different permission.I don't know what do those "wrdrrwrwrwrw*****" mean.So I need to search it .
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
This permission was forgotten.Now is ok
Firstly- Check that the SD card is mounted and readable.
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));
or
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY));
From your question here: You are using:
File file =new File(Environment.getExternalStorageDirectory(),"A.mp3");
mediaPlayer.setDataSource(file.getPath());
and have tried:
mediaPlayer.setDataSource("/storage/sdcard1/A.mp3")
Then try:
Edit: add in Music folder.
mediaPlayer.setDataSource("/sdcard1/Music/A.mp3");
Also: this http://www.bogotobogo.com/Android/android24Media.php#SDCard is an interesting link. So you can explore you files in Android Studio.
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 ...
I am downloading file from a server in Android using the DownloadManager class. I want to store this file in the internal memory of the device. I tried to use .setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory() +"/Android/data/xxx.xxx.xxx/files/") as mentioned here, but it is not working. How to solve my problem?
add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to manifest.xml and create "/Android/data/xxx.xxx.xxx/files/" path
try this
File testDirectory = new File(Environment.getExternalStorageDirectory() +"/Android/data/xxx.xxx.xxx/files/");
and
if (!testDirectory.exists()) {
testDirectory.mkdirs();
}
I want to create a file(not created) in a directory(not created) in the SDCARD.
How doing it ?
Thank you.
Try the following example:
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//handle case of no SDCARD present
} else {
String dir = Environment.getExternalStorageDirectory()+File.separator+"myDirectory";
//create folder
File folder = new File(dir); //folder name
folder.mkdirs();
//create file
File file = new File(dir, "filename.extension");
}
Don't forget to add the permission to your AndroidManifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The problem is that mkdirs() is called on a File object containing the whole path up to the actual file. It should be called on a File object containing the Path (the directory) and only that. Then you should use another File object to create the actual file.
You should also have to add permission to write to external media.
Add following line in the application manifest file, somewhere between <manifest> tags, but not inside <application> tag:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Use This:
DocumentFile tempDir = DocumentFile.fromSingleUri(context,targetDirectoryUri);
DocumentFile newDocumentFile = tempDir.createFile(type,name);
"targetDirectoryUri" is uri of the directory you want to put the file into it.
This is the only solution!
After Api 19 you can not write on SDCard, so you must use DocumentFile
instead File.
In addition, you must also take SDCard permission. To learn how to do this and get the targetDirectoryUri, please read this.
You can use in Kotlin
val file = File( this.getExternalFilesDir(null)?.getAbsolutePath(),"/your_image_path")
if (!file.exists()) {
file.mkdir()
}
Don't forget to give permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
See here why creating files on root of the SD card is a bad idea