accessing file on sdcard android

2019-05-30 00:56发布

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, "Title:"));

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!

3条回答
干净又极端
2楼-- · 2019-05-30 01:27

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.

查看更多
走好不送
3楼-- · 2019-05-30 01:29

@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.

查看更多
唯我独甜
4楼-- · 2019-05-30 01:35

Try

Environment.getExternalStorageDirectory()

to get the root of the sd-card.

查看更多
登录 后发表回答