how can I copy files to the file system of android?

前端 未结 5 840
深忆病人
深忆病人 2021-02-10 06:05

How can I copy files to the file system (place) in android? How can I access it?

5条回答
  •  感情败类
    2021-02-10 06:40

    I'm not sure if this is what you're asking, but here's another interpretation of your question:

    You can place files in the assets folder in the project on your development machine. They will be bundled into the apk file and then access them through the AssetManager class. Something like this from within a Service or Activity class:

    AssetManager am = getAssets();
    InputStream is = am.open("filename");
    

    This will allow you to read the contents of those files. I don't believe you can write to them though. There are other options for if you need to read and write files in your application's storage sandbox. This is done by calling openFileOutput(). I should note though that the user can always access the files on the device through ddms but other applications won't be able to access files stored in your application's storage sandbox.

    Edit Based on your comment, you probably want to use

    OutputStream out = openFileOutput("outfile");
    

    to write to a file from your code. This code must be called from within an Activity or Service (something that extends Context). This file will be stored on the phone's file system and not on the sdcard. These files will not be accessible to the average user. If users know how to enable usb debugging then they will be able to access them and there is really no way around this.

提交回复
热议问题