Android Write file to accessible folder for windows usb file transfer

房东的猫 提交于 2019-12-11 03:55:38

问题


I am operating with a standard Nexus 5 which is un-rooted with standard vanilla android 4.4.2

I would like to create a new file that is accessible in Windows through the standard USB connector. I don't want to root my phone.

Here is a snippet of my code:

File outputFile = new File(new File(getFilesDir().getParentFile(), "assets"), "example.xml");

Does anyone know of any directories that are writable in app + readable via USB transfer ?

Thank you for any help ! :)


回答1:


You can create the file on the External Storage like this:

String fileName = "example.xml";
String myDirectory = "myDirectory";
String externalStorage = Environment.getExternalStorageDirectory().getAbsolutePath();

File outputFile = new File(externalStorage + File.separator + myDirectory + File.separator + fileName);



回答2:


Turns out there is not a automatic filesystem refresh for files displayed through USB, in my case I used a android file manager to view the files before they became visible over USB. The above answer supported me in getting to this answer.




回答3:


I was also looking to do something similar as OP Snake. I used

Environment.getExternalStorageDirectory().getAbsolutePath()

to place a file in the top directory when opening Windows Explorer and going to my Android device.

However, the following post made it clear that you have to reboot your phone before you can actually see the newly created file. I had to do this extra step as well.

Source: https://android.stackexchange.com/questions/63640/how-to-access-storage-emulated-0-from-pc



来源:https://stackoverflow.com/questions/22304173/android-write-file-to-accessible-folder-for-windows-usb-file-transfer

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