保存到SD卡中的奇巧 [英] Saving to SD card in KitKat

查看:255
本文介绍了保存到SD卡中的奇巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作正在使用的执法Android平板电脑应用程序。我有平板备份所有的数据到外部SD卡。我希望能够从的情况下,当警察被打击犯罪的平板电脑被你摧毁卡恢复数据。我用这个code存储备份:

 文件fil​​e =新的文件(到/ mnt / extSdCard /+ NewOrLoad.directoryName +settings.dat);
    尝试{
        BW =新的BufferedWriter(新的FileWriter(文件));
        GSON GSON =新GSON();
        JSON字符串= gson.toJson(dreform);
        bW.write(JSON);
        bW.close();
    }赶上(IOException异常五){
        e.printStackTrace();
    }
    sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(文件://+ Environment.getExternalStorageDirectory())));

这工作得很好。不幸的是平板电脑购买最新批次正在运行奇巧,这code不工作了。该唯一的解决办法,我发现涉及生根的牌位,这仅仅是不可行的在我的情况。有没有解决办法?


解决方案

  

它能正常工作


只为的/ mnt / extSdCard 存在并且是可写的设备。这是Android SDK的边界之外,并没有保证任何设备将兑现的具体路径。也许你是在受控环境中,一台设备模型,其中使这一假设是(相对)安全的。


  

有没有解决办法?


由于323go在评论中提到,欢迎您的<一个href=\"https://developer.android.com/reference/android/content/Context.html#getExternalFilesDirs%28java.lang.String%29\"相对=nofollow>使用 getExternalFilesDirs() (注意是复数)。如果在生成的列表中一个以上的元素,第二个和随后的条目将是第二外部存储,如可移动卡。你可以阅读并从该目录写的,没有任何权限。

还有 ContextCompat ,它提供了一个 getExternalFilesDirs()的实施,所有的工作方式回到API级别4.将只回报设备一个值运行速度比API级别19较少,但它可以让你跳过版本检查自己,因为大量的API等级19+设备也只会返回一个值(例如,他们没有填充卡SD卡插槽)。

我最近写了一篇一系列博客文章试图澄​​清存储情况。你可能会在href=\"http://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html\" rel=\"nofollow\">移动存储<一上

I am working on an Android tablet application that is being used by law enforcement. I had the tablet backup all the data to an external SD card. I wanted to be able to recover data from the card in case the tablet got destroyed while the police officers were fighting crime. I used this code to store the backup:

File file = new File("/mnt/extSdCard/" + NewOrLoad.directoryName + "settings.dat");
    try {
        bW = new BufferedWriter(new FileWriter(file)); 
        Gson gson = new Gson();
        String json = gson.toJson(dreform); 
        bW.write(json);
        bW.close(); 
    } catch (IOException e) {
        e.printStackTrace();
    }
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

It worked fine. Unfortunately the newest batch of tablets purchased are running KitKat and this code doesn't work anymore. The only solutions I found involve rooting the tablets, which is just not feasible in my case. Is there a workaround?

It worked fine

Only for devices where /mnt/extSdCard existed and was writeable. This is outside the bounds of the Android SDK, and there is no guarantee that any device will honor that specific path. Perhaps you were in a controlled environment, with a single device model, where making this assumption was (relatively) safe.

Is there a workaround?

As 323go mentions in the comments, you are welcome to use getExternalFilesDirs() (note the plural). If there is more than one element in the resulting list, the second and subsequent entries will be for "secondary external storage" such as removable cards. You can read and write from this directory without any required permissions.

There is also ContextCompat, which offers a getExternalFilesDirs() implementation that works all the way back to API Level 4. It will only ever return one value on devices running less than API Level 19, but it allows you to skip version checking yourself, as plenty of API Level 19+ devices will also only return one value (e.g., they do not have an SD card slot populated with a card).

I recently wrote a series of blog posts attempting to clarify the storage situation. You may be particularly interested in the one on removable storage.

这篇关于保存到SD卡中的奇巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆