[Android] [PATCH 1/2] utils: Use more Uri

Thomas Guillem thomas at gllm.fr
Fri Jun 5 13:57:30 CEST 2015


---
 libvlc/src/org/videolan/libvlc/util/AndroidUtil.java | 20 +++++++++++++-------
 vlc-android/src/org/videolan/vlc/MediaLibrary.java   |  2 +-
 .../src/org/videolan/vlc/gui/audio/AudioUtil.java    |  4 ++--
 .../videolan/vlc/gui/video/VideoPlayerActivity.java  |  4 ++--
 4 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/libvlc/src/org/videolan/libvlc/util/AndroidUtil.java b/libvlc/src/org/videolan/libvlc/util/AndroidUtil.java
index a024203..8ade8f2 100644
--- a/libvlc/src/org/videolan/libvlc/util/AndroidUtil.java
+++ b/libvlc/src/org/videolan/libvlc/util/AndroidUtil.java
@@ -66,22 +66,28 @@ public class AndroidUtil {
         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
     }
 
-    public static File URItoFile(String URI) {
-        if (URI == null) return null;
-        return new File(Uri.decode(URI).replaceFirst("file://", ""));
+    public static File UriToFile(Uri uri) {
+        if (uri == null) throw new NullPointerException("Cannot convert null uri!");
+        return new File(uri.getPath().replaceFirst("file://", ""));
     }
 
     /**
      * Quickly converts path to URIs, which are mandatory in libVLC.
      *
-     * @param path
-     *            The path to be converted.
+     * @param path The path to be converted.
      * @return A URI representation of path
      */
-    public static String PathToURI(String path) {
+    public static Uri PathToUri(String path) {
         if (path == null) {
             throw new NullPointerException("Cannot convert null path!");
         }
-        return Uri.fromFile(new File(path)).toString();
+        return Uri.fromFile(new File(path));
+    }
+
+    public static Uri FileToUri(File file) {
+        if (file == null) {
+            throw new NullPointerException("Cannot convert null file!");
+        }
+        return Uri.fromFile(file);
     }
 }
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/MediaLibrary.java b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
index 85e555f..afe9ce6 100644
--- a/vlc-android/src/org/videolan/vlc/MediaLibrary.java
+++ b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
@@ -355,7 +355,7 @@ public class MediaLibrary {
 
                 // Process the stacked items
                 for (File file : mediaToScan) {
-                    String fileURI = AndroidUtil.PathToURI(file.getPath());
+                    String fileURI = AndroidUtil.FileToUri(file).toString();
                     if (mBrowser != null && mBrowser.get() != null)
                         mBrowser.get().sendTextInfo(file.getName(), count,
                                 mediaToScan.size());
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
index b9c2178..aa08f45 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
@@ -76,7 +76,7 @@ public class AudioUtil {
     public static String PLAYLIST_DIR = null;
 
     public static void setRingtone(MediaWrapper song, Context context){
-        File newringtone = AndroidUtil.URItoFile(song.getLocation());
+        File newringtone = AndroidUtil.UriToFile(song.getUri());
         if(newringtone == null || !newringtone.exists()) {
             Toast.makeText(context.getApplicationContext(),context.getString(R.string.ringtone_error), Toast.LENGTH_SHORT).show();
             return;
@@ -220,7 +220,7 @@ public class AudioUtil {
     }
 
     private static String getCoverFromFolder(MediaWrapper media) {
-        File f = AndroidUtil.URItoFile(media.getLocation());
+        File f = AndroidUtil.UriToFile(media.getUri());
         if (f == null)
             return null;
 
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 55b883f..6157a9a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -2697,7 +2697,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
                             while((bytesRead = is.read(buffer)) >= 0) {
                                 os.write(buffer, 0, bytesRead);
                             }
-                            mLocation = AndroidUtil.PathToURI(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY + "/Download/" + filename);
+                            mLocation = AndroidUtil.PathToUri(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY + "/Download/" + filename).toString();
                         }
                     } catch (Exception e) {
                         Log.e(TAG, "Couldn't download file from mail URI");
@@ -2716,7 +2716,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
                         if (cursor != null) {
                             int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                             if (cursor.moveToFirst())
-                                mLocation = AndroidUtil.PathToURI(cursor.getString(column_index));
+                                mLocation = AndroidUtil.PathToUri(cursor.getString(column_index)).toString();
                             cursor.close();
                         }
                         // other content-based URI (probably file pickers)
-- 
2.1.4



More information about the Android mailing list