简体   繁体   English

使用位于原始或资产文件夹中的文件

[英]Using a file located in either the raw or assets folder

Currently I am able to download a file off the internet and store on the SD card, then use the file from there. 目前,我可以从互联网上下载文件并将其存储在SD卡上,然后从那里使用该文件。 However that makes the file (with proprietary data) available to be seen. 但是,这使得该文件(带有专有数据)可供查看。 I would prefer to use the file from somewhere like raw or assets folder. 我更喜欢从原始或资产文件夹之类的地方使用该文件。 I will skip the downloading code, but my code to use the file is this 我将跳过下载代码,但是我使用文件的代码是这个

  File myFile = new File (android.os.Environment.getExternalStorageDirectory() + "/folder/filename.xml");
  Intent myIntent = new Intent(Intent.ACTION_VIEW);
  myIntent.setData(Uri.fromFile(myFile));

Android opens the file with the default application and all is good. Android使用默认应用程序打开文件,一切正常。

I have found similar Q/A's that revolve around using code like 我发现类似的Q / A涉及使用诸如

  Uri.parse("android.resource://com.projectname.testing/raw/filename");

and

  InputStream ins  = getResources().openRawResource(R.raw.filename);

but I can't work out how to get either of those two back into a 'file' format to be used with my .setData code 但我不知道如何将这两个中的任何一个恢复为'file'格式以与我的.setData代码一起使用

I would like to solve my problem by simply accessing the file as a file. 我想通过简单地将文件作为文件访问来解决我的问题。 However since it is being used by an external application I have read I might need to make a temporary copy of the file with mode_world_readable then delete it after the application closes. 但是,由于我已经阅读了外部应用程序正在使用的文件,因此我可能需要使用mode_world_read制作文件的临时副本,然后在应用程序关闭后将其删除。 This sounds like a lot of extra work, especially since my code above does work for a file stored on the SD card. 这听起来像是很多额外的工作,特别是因为上面的代码对于存储在SD卡上的文件确实有效。

Does anyone have any ideas? 有人有什么想法吗? Thanks 谢谢

I would prefer to use the file from somewhere like raw or assets folder. 我更喜欢从原始或资产文件夹之类的地方使用该文件。

Note that these too can be "seen". 请注意,这些也可以被“看到”。

but I can't work out how to get either of those two back into a 'file' format to be used with my .setData code 但我不知道如何将这两个中的任何一个恢复为'file'格式以与我的.setData代码一起使用

setData() does not take a File . setData()不使用File It takes a Uri . 它需要一个Uri Use Uri.parse() to parse other types of Uri values -- you already have this code shown above. 使用Uri.parse()解析其他类型的Uri值-上面已经显示了此代码。

However since it is being used by an external application I have read I might need to make a temporary copy of the file with mode_world_readable then delete it after the application closes. 但是,由于我已经阅读了外部应用程序正在使用的文件,因此我可能需要使用mode_world_read制作文件的临时副本,然后在应用程序关闭后将其删除。

It definitely will need to be world-readable. 它肯定需要是世界可读的。 Also, not all apps support all schemes, so apps that support file:// or http:// might not support android.resource:// . 另外,并非所有应用都支持所有方案,因此支持file://http://可能不支持android.resource://

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

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