Android:替代已弃用的 Context.MODE_WORLD_READABLE? [英] Android: Alternative to the deprecated Context.MODE_WORLD_READABLE?

查看:101
本文介绍了Android:替代已弃用的 Context.MODE_WORLD_READABLE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自在这里我知道一种写入文件的方法,并且其他应用程序和其他意图都可以访问它,但是现在不推荐使用 Context.MODE_WORLD_READABLE,我该如何安全地完成此操作?

From here I know a way to write a file and be accessible to other app and other intent, but now that the Context.MODE_WORLD_READABLE is deprecated how can I safely accomplish this?

FileOutputStream out = myActivity.openFileOutput(fileTo, Context.MODE_WORLD_READABLE);

好的更多信息:

我正在使用这个:

intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");

uri 将是我将写入 SD 卡位置的那个.视频将来自应用程序,所以问题是,如果现在不允许这样做,我该如何编写文件并查看它.

And the uri will be the one that I will write to a sdcard location. And the video will come from the application so problem is, if this is now not allowed, how can I write the file and view it.

推荐答案

uri 将是我将写入 SD 卡位置的那个.

And the uri will be the one that I will write to a sdcard location.

默认情况下已经是MODE_WORLD_WRITABLE.另请注意,您列出的代码(openFileOutput())不会写入外部存储(您错误地称之为sdcard").openFileOutput() 用于内部存储.

That is already MODE_WORLD_WRITABLE by default. Also note that the code you have listed (openFileOutput()) does not write to external storage (what you incorrectly call "sdcard"). openFileOutput() is for internal storage.

视频将来自应用程序,所以问题是,如果现在不允许这样做,我该如何编写文件并查看它.

And the video will come from the application so problem is, if this is now not allowed, how can I write the file and view it.

如果您真的要将文件写入外部存储,只需使用指向该文件的 Uri.

If you are really writing the file to external storage, just use a Uri pointing to that file.

如果您要将文件写入内部存储,请创建一个 ContentProvider 来提供该文件,并使用指向该 ContentProviderUri.这是一个带有 ContentProvider 的示例应用程序code> 在第一次运行时从 assets/ 中提取 PDF 文件,然后通过 openFile() 提供该文件,以便 PDF 查看器可以查看.

If you are writing the file to internal storage, create a ContentProvider to serve that file, and use a Uri pointing to that ContentProvider. Here is a sample application with a ContentProvider that extracts a PDF file from assets/ on first run, then serves up that file via openFile() so it can be viewed by a PDF viewer.

这篇关于Android:替代已弃用的 Context.MODE_WORLD_READABLE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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