开发者

How to open a local app file in another app via an intent?

To preface my question, couple things to note. I don't want to store said file on the sdcard in this case. The file also cannot be storage directly in the apps local files directory. It needs to be in a subdirectory, so it 开发者_高级运维cannot write the file using openFileOutput() and MODE_WORLD_READABLE.

The app may download files small files like pdfs and store them locally in a subdirectory. I would like to be able to have the user open these files if they have an app that can open them.

For example here is an intent for sending a pdf:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(path), "application/pdf");
startActivity(intent);

path being something like: /data/data/packagename/files/subdir/example.pdf

That intent will open a pdf viewer, but the viewer is unable to open the file. I assume this is a permissions issue. I tried Mark Murphy's suggestion here: http://groups.google.com/group/android-developers/browse_thread/thread/4e55d869213483a9/b7270078ac1a2744?lnk=raot of using Runtime.getRuntime().exec("chmod 755 " + fileName); but it didn't make any difference. He also suggested a Content Provider but I would like to avoid it if I can because it seems like a lot just to get this file over to another app.

If the content provider is the only option, do I have to save the file to the content provider or can I just use the content provider as a pass through to get it to the other app when I need to?

Thanks, let me know if I'm not being clear.


I also tried all the options you mentioned (and more) and a content provider was the only way that worked for me. I'm not exactly sure what you mean by "save the file to the content provider", but you don't actually need anything in a database for the provider to serve up the file to the other application.


You might like to look at the android:sharedUserId manifest property:

The name of a Linux user ID that will be shared with other applications. By default, Android assigns each application its own unique user ID. However, if this attribute is set to the same value for two or more applications, they will all share the same ID — provided that they are also signed by the same certificate. Application with the same user ID can access each other's data and, if desired, run in the same process.

Be aware that your applications already have a default one that you can't determine (AFAIK). It may be as well to define the same sharedUserId for both apps, then install them on a new emulator.

As a second point, are you sure that you will be able to view a PDF file? I thought that there was no support for it in Android yet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜