accessing file on sdcard android

孤者浪人 提交于 2019-12-08 04:09:39

问题


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!


回答1:


Try

Environment.getExternalStorageDirectory()

to get the root of the sd-card.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/5308997/accessing-file-on-sdcard-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!