共享文件或内容与第三方活动 [英] Sharing files or content with 3rd party activities
问题描述
我有一个简单的问题,但似乎无法在任何地方我看网上找到了答案。
我有活性的,从互联网下载文件,并将它们通过getApplicationContext()获得一个本地文件夹getFilesDir()getPath(),它指向:/data/data/com.myapp.android/文件
现在,当用户选择我要显示其内容的文件,并为我做了以下内容:
文件文件=新的文件(文件路径);
开放的我们的uri = Uri.fromFile(文件);
MimeTypeMap哑剧= MimeTypeMap.getSingleton();
字符串类型= mime.getMimeTypeFromExtension(fileExtension);
意向意图=新的意图(Intent.ACTION_VIEW);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(URI,类型);
意图intentChooser = Intent.createChooser(意向,打开文件);
startActivity(intentChooser);
在此执行用户psented的打开文件对话框$ P $,他们选择后,画廊的展示活动将出现什么就可以了。
在logcat的样子,我看到以下内容:
E / PanoMetadata(17732):无法读取文件:/data/data/com.myapp.android/files/downloads/Splash.png
E / PanoMetadata(17732):java.io.FileNotFoundException:/data/data/com.myapp.android/files/downloads/Splash.png:打开失败:EACCES(权限被拒绝)
E / PanoMetadata(17732):在libcore.io.IoBridge.open(IoBridge.java:409)
E / PanoMetadata(17732):在java.io.FileInputStream中< INIT>(FileInputStream.java:78)。
请注意在打开失败:EACCES(权限被拒绝)
不过,该文件是真的在那里,我可以用我的活动一个没有问题显示出来。
这个问题似乎是,它开始使用ACTION_VIEW(在这种情况下,图库)的其他活动似乎在不同的进程/用户运行,所以它并没有给我的文件进行读取访问。
我的三个问题是:
-
是否有可能得到ACTION_VIEW活动中我的过程和用户上下文(活动A的)?
运行
-
我如何活动之间共享文件? (我不喜欢将文件复制到/ SD卡/ ...但如果比它必须是唯一的选择。)
-
有没有办法从我的活动传递文件的内容(而不是文件指针)图库活动或其他活动?
商祺!
是否有可能得到ACTION_VIEW活动中我的过程和用户上下文(活动A的)?
运行
没有,你也不希望它,出于安全原因。
我如何活动之间共享文件?
这个问题是与您的下一个,据我可以告诉。
有没有办法从我的活动传递文件的内容(而不是文件指针)图库活动或其他活动?
最好的解决办法是使用的ContentProvider
。最简单的解决方法是使用 FileProvider
,您可以直接添加到您的清单和配置来提供文件,而无需创建一个子类。另请参阅: http://developer.android.com/training/secure-文件共享/ index.html的
I have a simple question but can't seem to find the answer anywhere I look online.
I have an activity A that downloads files from the internet and puts them to a local folder obtained by getApplicationContext().getFilesDir().getPath() which points to: "/data/data/com.myapp.android/files"
Now when the user selects a file I want to show its content and for that I do the following:
File file = new File(filePath);
Uri uri = Uri.fromFile(file);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(fileExtension);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri, type);
Intent intentChooser = Intent.createChooser(intent, "Open File");
startActivity(intentChooser);
When this executes the user is presented with the Open File dialog and after they select "Gallery" the Gallery activity appears with nothing on it.
Looking at logcat I see the following:
E/PanoMetadata(17732): Could not read file: /data/data/com.myapp.android/files/downloads/Splash.png
E/PanoMetadata(17732): java.io.FileNotFoundException: /data/data/com.myapp.android/files/downloads/Splash.png: open failed: EACCES (Permission denied)
E/PanoMetadata(17732): at libcore.io.IoBridge.open(IoBridge.java:409)
E/PanoMetadata(17732): at java.io.FileInputStream.<init>(FileInputStream.java:78)
Note the "open failed: EACCES (Permission denied)"
But the file is really there and I can display it using my activity A without a problem.
The problem seems to be that the other activity which starts using ACTION_VIEW (in this case Gallery) seems to run under different process/user so it does not have read access to my file.
My 3 questions are:
Is it possible to get the ACTION_VIEW activity to run within my process and user context (of activity A)?
How do I share files between activities? (I do not like to copy the file under /sdcard/... but if that is the only option than it will have to be.)
Is there a way to pass the file content (not file pointer) from my activity to the Gallery activity or other activities?
Regards!
Is it possible to get the ACTION_VIEW activity to run within my process and user context (of activity A)?
No, nor would you want it to, for security reasons.
How do I share files between activities?
This question is the same as your next one, as far as I can tell.
Is there a way to pass the file content (not file pointer) from my activity to the Gallery activity or other activities?
The best solution is to use a ContentProvider
. The simplest solution is to use FileProvider
, which you can just add to your manifest and configure to serve up your files, without any need to create a subclass. See also: http://developer.android.com/training/secure-file-sharing/index.html
这篇关于共享文件或内容与第三方活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!