accessing file on sdcard android
I am new to Android development. I am trying to use the following code to email a file on my motorola milestone through gmail.
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("audio/mp3");
sendIntent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/king1.mp3");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
sendIntent.putExtra(Intent.EXTRA_TEXT, "this is the email content2");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/king1.mp3"));
startActivity(Intent.createChooser(sendIntent, "Ti开发者_StackOverflow社区tle:"));
Running the code sends the email but the attachment sent is of 0kb. I have seen this problem elsewhere on the internet but I am not sure if I am declaring the correct path to the file. How can I know the exact path of the file? If I mount it, the path I get is /Volumes/NO NAME/king1.mp3.
Or
Do I need to read the file using fileinputstream first?
Thank you so much!
Try
Environment.getExternalStorageDirectory()
to get the root of the sd-card.
@sanna is correct that you should use Environment.getExternalStorageDirectory()
, however, I would suggest first using Environment.getExternalStorageState()
to determine if you are able to access the storage - for instance, in your example, when the SD card is mounted on the PC.
On my device the external sd data is in a further directory called 'external_sd'. Environment.getExternalStorageDirectory() gives the root directory for the sd card, but if you placed data on your device from a computer, it may be in "file://"+Environment.getExternalStorageDirectory()+"/external_sd/"
This was only obvious by using the 'My Files' app on the device, the external_sd directory level is not apparent when mounted on a computer via USB.
精彩评论