[Android] Handle FileProvider URIs

Geoffrey Métais git at videolan.org
Tue May 19 13:41:36 CEST 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue May 19 12:21:05 2015 +0200| [b2c89ab504519a3f2fd2a4705dd43d72804fd795] | committer: Geoffrey Métais

Handle FileProvider URIs

Fixes #14501,#14451

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=b2c89ab504519a3f2fd2a4705dd43d72804fd795
---

 .../vlc/gui/video/VideoPlayerActivity.java         |   40 +++++++++++++++++---
 1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index b3bc9c9..a0fe143 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -46,8 +46,10 @@ import android.os.Bundle;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
+import android.os.ParcelFileDescriptor;
 import android.preference.PreferenceManager;
 import android.provider.MediaStore;
+import android.provider.OpenableColumns;
 import android.provider.Settings;
 import android.provider.Settings.SettingNotFoundException;
 import android.support.v4.app.FragmentManager;
@@ -120,6 +122,7 @@ import org.videolan.vlc.widget.OnRepeatListener;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -859,7 +862,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
 
     private static void start(Context context, String location, String title, boolean fromStart, int openedPosition) {
         Intent intent = new Intent(context, VideoPlayerActivity.class);
-        intent.setAction(VideoPlayerActivity.PLAY_FROM_VIDEOGRID);
+        intent.setAction(PLAY_FROM_VIDEOGRID);
         intent.putExtra(PLAY_EXTRA_ITEM_LOCATION, location);
         intent.putExtra(PLAY_EXTRA_ITEM_TITLE, title);
         intent.putExtra(PLAY_EXTRA_FROM_START, fromStart);
@@ -897,9 +900,11 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
 
     private void exit(int resultCode){
         Intent resultIntent = new Intent(ACTION_RESULT);
-        resultIntent.setData(Uri.parse(mLocation));
-        resultIntent.putExtra(EXTRA_POSITION, mLibVLC.getTime());
-        resultIntent.putExtra(EXTRA_DURATION, mLibVLC.getLength());
+        if (mLocation != null) {
+            resultIntent.setData(Uri.parse(mLocation));
+            resultIntent.putExtra(EXTRA_POSITION, mLibVLC.getTime());
+            resultIntent.putExtra(EXTRA_DURATION, mLibVLC.getLength());
+        }
         setResult(resultCode, resultIntent);
         finish();
     }
@@ -2636,6 +2641,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
      * External extras:
      * - position (long) - position of the video to start with (in ms)
      */
+    @TargetApi(12)
     @SuppressWarnings({ "unchecked" })
     private void loadMedia() {
         mLocation = null;
@@ -2705,7 +2711,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
                     }
                 }
                 // Media or MMS URI
-                else {
+                else if (TextUtils.equals(data.getAuthority(), "media")){
                     try {
                         Cursor cursor = getContentResolver().query(data,
                                 new String[]{ MediaStore.Video.Media.DATA }, null, null, null);
@@ -2725,6 +2731,30 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
                             mLocation = "file://"+mLocation;
                         Log.e(TAG, "Couldn't read the file from media or MMS");
                     }
+                } else {
+                    ParcelFileDescriptor inputPFD = null;
+                    try {
+                        inputPFD = getContentResolver().openFileDescriptor(data, "r");
+                        if (LibVlcUtil.isHoneycombMr1OrLater())
+                            mLocation = "fd://"+inputPFD.getFd();
+                        else {
+                            String fdString = inputPFD.getFileDescriptor().toString();
+                            mLocation = "fd://" + fdString.substring(15, fdString.length()-1);
+                        }
+
+                        Cursor returnCursor =
+                                getContentResolver().query(data, null, null, null, null);
+                        if (returnCursor != null) {
+                            int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
+                            int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
+                            returnCursor.moveToFirst();
+                            title = returnCursor.getString(nameIndex);
+                        }
+                    } catch (FileNotFoundException e) {
+                        Log.e(TAG, "Couldn't understand the intent");
+                        encounteredError();
+                        return;
+                    }
                 }
             } /* External application */
             else if (intent.getDataString() != null) {



More information about the Android mailing list