Question

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:

  1. Is it possible to get the ACTION_VIEW activity to run within my process and user context (of activity A)?

  2. 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.)

  3. Is there a way to pass the file content (not file pointer) from my activity to the Gallery activity or other activities?

Regards!

Était-ce utile?

La solution

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

Autres conseils

An app can't acces the private data of another app. Being that way, "Gallery" can't reach "data/data/com.myapp.android/files". You need to put the shared files directory in the external storage.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top