简体   繁体   中英

writing file on sdcard privately for application android

I am writing audio data as in amr file format in sdcard.

    File file = new File(dir, "test.amr");
    try {
        FileOutputStream f = new FileOutputStream(file);
        f.write(audioData);
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

Its working properly but my problem is it shows in other applications as well like in default media player of android OS.

My question is how can write a file on sdcard which is only accessible to my application.Not to other applications on device.

Thanks.

Files on SD Card, some way or another, will be accessible by all, you cannot change that. You should use the internal storage

You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user).

That's not possible, all files on SD card are accessible to all apps.

To keep files private, create them in application's local directory. Location of app's private folder is determined by:

String dir = getPackageManager().getPackageInfo("com.example.app", 0).applicationInfo.dataDir;

Other option: make empty ".nomedia" file in your folder on SD card, then files inside won't (should not) show in media players.

I think it is possible on API 9(android 2.3) or higher. From 2.3, there comes a new Class called StorageManager . The OBB filesystem may meet your demand. Sorry, I am a greenhand on Android and I can not give you a good example, however, you can See the official document here

Create a folder on SD card, store your files there.

And create a file named .nomedia in that folder to hide the files in that folder from being listed in other apps.

Any folder containing a file named .nomedia won't be scanned by MediaServer and thus won't be listed in Music player etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM