MediaPlayer - can't find path of sdcard on real device - android

On my code I use:
mp = new MediaPlayer();
String filePath = Environment.getExternalStorageDirectory().getPath() + "/mymusic/asong.mp3";
try {
mp.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
And on the emulator the song.mp3 is played normally. But when I test it on my real device, it gives an error (-38, 0). That means it can not find the path of the song. I connect the usb cable, go to my Computer, GT-I8260 and paste the folder "mymusic" (that contains asong.mp3) under "Card" folder (where an empty folder named "LOST.DIR" is also placed). But why doesn't it work? Thanks a lot

it's card but at least
Environment.getExternalStorageDirectory() + "/mymusic/asong.mp3";
is enough.
Make sure it exists because you may not have the folder created before.
File f = new File(Environment.getExternalStorageDirectory() + "/mymusic");
if (!f.exists()) { f.mkdirs(); }
also make sure that it's not mounted while writing, since it may happen that it is not accessable at all.
Also revalidate that you have setupted the manifests permission to read/write the external storage

Related

How to check a file before delete in android

I know how to delete a file, it's:
File file = new File(path);
file.delete();
Can I test that the file is currenlty in use without rooting my device?
For exmple I want to check if the file is open before i can delete it.
I want to be able to catch errors with a try catch sentence.
You can use a try/catch clause.
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}
You can find more information here.

How to copy files in Android Storage?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I am trying to copy .jpg file to and from in Storage folders.
try {
Process c = Runtime.getRuntime().exec("dd if="+path+
"of="+Environment.getExternalStorageDirectory()+"/1.jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
path contains the exact image location in storage ! But it doesn't work. It can't copy the image to another folder of storage. Please kindly help me to solve this in every possible way! Thank you.
try {
String cmd="dd if="+path+" of="+Environment.getExternalStorageDirectory()+"/1.jpg";
Runtime.getRuntime().exec(cmd+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
It happens to me sometimes and I use
Environment.getExternalStorageDirectory().getPath()
Have you tried that?

Can't write to /mnt/sdcard

This one has me stumped (Android 4.3)
I have the right permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
... I'm explicitly creating a path to /storage/sdcard0/path/to/data.csv and the FileOutputStream doesn't throw any exceptions.
I tried reading the path via Environment.getExternalStorageDirectory() but that has some "emulated" path.
In either case, I can't see the files on the filesystem ...
What's more confusing is that MEDIA_MOUNTED_READ_ONLYis mounted_ro ... but this isn't the case .
It looks like another app us using /mnt/extSdCard... I tried hard coding that in my app but that doesn't work either. Even the directory is missing. This is very confusing.
I'm calling mkdirs() on this of course and the FileOutputStream is created....
What am I doing wrong?
Try the following code, and see where it fails?
try {
File mPath = new File(android.os.Environment.getExternalStorageDirectory(),"path/to");
if (!mPath.exists()) {
if(!mPath.mkdirs()) {
Log.e(getClass().getName(), "Can't Create Folder: "+mPath.getName());
}
}
File mLogFile = new File(mPath, "data.csv");
FileWriter mFileWriter = null;
if(mPath.canWrite()) {
mFileWriter = new FileWriter(mLogFile, true);
mBufferedWriter = new BufferedWriter(mFileWriter);
}
else {
Log.e(getClass().getName(), "Can't write under Folder: "+mPath.getName());
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

Delete files starting with particular word

In my application some temporary files are created,and its name starts with 'temp'. How to delete these files from sd card on exit .
you can delete files from SDCARD as:
File folder = new File(Environment.getExternalStorageDirectory() + "/tempdirname");
try {
File[] filenamestemp = folder.listFiles();
for(int i=0;i<filenamestemp.length;i++){
if(filenamestemp[i].getAbsolutePath().toString().contains("temp"))
filenamestemp[i].delete();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and make sure you have added SDCARD access permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Can't access written files on windows

I wrote a logfile to my sd (internal storage of the Samsung Galaxy Nexus). Actually the file is invisible in windows. How can I let users access them?
this.state = Environment.getExternalStorageState();
Returns "mounted"
This is my Code:
/**
* Creates a new instance of Logger
*
* */
public Logger() {
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath();
this.file = new File(path, "log.txt");
}
/**
* Writes results to log file
*
* **/
public void log(boolean isConnected, int signalStrength) {
try {
this.buf = new BufferedWriter(new FileWriter(this.file, true));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Calendar time = Calendar.getInstance();
String result = time.getTime().toString() + " "
+ String.valueOf(isConnected) + " "
+ String.valueOf(signalStrength);
this.buf.write(result);
this.buf.flush();
this.buf.close();
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
It won't write anything (even though I receive no exception).
Manifest contains:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Edit:
File path = Environment.getExternalStorageDirectory()
.getAbsoluteFile();
Didn't help.
The file is there, but it has not been picked up by the MediaScanner yet. You can verify by opening a shell like so:
adb shell ls -l /mnt/sdcard/clown.txt
You need to either wait for it to be picked up, reboot, or (preferably), call MediaScannerConnection.scanFile() when you are done writing to the file.

Categories

Resources