简体   繁体   English

在sdcard上私下为应用程序android写入文件

[英]writing file on sdcard privately for application android

I am writing audio data as in amr file format in sdcard. 我正在以sdcard中的amr文件格式写入音频数据。

    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. 它工作正常,但我的问题是它在其他应用程序中显示,以及在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. 我的问题是如何在sdcard上写一个文件,该文件只能由我的应用程序访问,而不能由设备上的其他应用程序访问。

Thanks. 谢谢。

Files on SD Card, some way or another, will be accessible by all, you cannot change that. 所有人都可以访问SD卡上的文件,您无法更改。 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. 这是不可能的,所有应用都可以访问SD卡上的所有文件。

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. 其他选择:在SD卡上的文件夹中创建一个空的“ .nomedia”文件,然后其中的文件不会(不应)显示在媒体播放器中。

I think it is possible on API 9(android 2.3) or higher. 我认为可以在API 9(android 2.3)或更高版本上使用。 From 2.3, there comes a new Class called StorageManager . 从2.3开始,有一个名为StorageManager的新类。 The OBB filesystem may meet your demand. OBB文件系统可能满足您的需求。 Sorry, I am a greenhand on Android and I can not give you a good example, however, you can See the official document here 抱歉,我是Android上的老手,不能给您一个很好的例子,但是,您可以在此处查看官方文档

Create a folder on SD card, store your files there. 在SD卡上创建一个文件夹,在其中存储文件。

And create a file named .nomedia in that folder to hide the files in that folder from being listed in other apps. 然后在该文件夹中创建一个名为.nomedia的文件,以隐藏该文件夹中的文件,使其不会在其他应用程序中列出。

Any folder containing a file named .nomedia won't be scanned by MediaServer and thus won't be listed in Music player etc. 包含名为.nomedia的文件的任何文件夹都不会被MediaServer扫描,因此不会在音乐播放器等中列出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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