[Android] Only decode filename from uri when it's actually used

Sébastien Toque git at videolan.org
Wed Apr 4 22:14:53 CEST 2012


android | branch: master | Sébastien Toque <xilasz at gmail.com> | Wed Apr  4 20:59:12 2012 +0200| [2b74b9f5fc12d40b39b7c6dbbf206f02878cc0fa] | committer: Sébastien Toque

Only decode filename from uri when it's actually used

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=2b74b9f5fc12d40b39b7c6dbbf206f02878cc0fa
---

 .../src/org/videolan/vlc/DatabaseManager.java      |    6 ++----
 vlc-android/src/org/videolan/vlc/Media.java        |    9 ++++++---
 vlc-android/src/org/videolan/vlc/Util.java         |    6 ++++++
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/DatabaseManager.java b/vlc-android/src/org/videolan/vlc/DatabaseManager.java
index 162445d..8e57057 100644
--- a/vlc-android/src/org/videolan/vlc/DatabaseManager.java
+++ b/vlc-android/src/org/videolan/vlc/DatabaseManager.java
@@ -305,8 +305,7 @@ public class DatabaseManager {
                         picture = BitmapFactory.decodeByteArray(blob, 0, blob.length);
                     }
                     String location = cursor.getString(8);
-                    File file = Util.URItoFile(location);
-                    Media media = new Media(context, location, file.getName().substring(0, file.getName().lastIndexOf('.')),
+                    Media media = new Media(context, location,
                             cursor.getLong(0), cursor.getLong(1), cursor.getInt(2),
                             picture, cursor.getString(4),
                             cursor.getString(5), cursor.getString(6),
@@ -353,8 +352,7 @@ public class DatabaseManager {
             if (blob != null) {
                 picture = BitmapFactory.decodeByteArray(blob, 0, blob.length);
             }
-            File file = Util.URItoFile(location);
-            media = new Media(context, location, file.getName().substring(0, file.getName().lastIndexOf('.')),
+            media = new Media(context, location,
                     cursor.getLong(0), cursor.getLong(1), cursor.getInt(2),
                     picture, cursor.getString(4),
                     cursor.getString(5), cursor.getString(6),
diff --git a/vlc-android/src/org/videolan/vlc/Media.java b/vlc-android/src/org/videolan/vlc/Media.java
index db238ae..880f3bf 100644
--- a/vlc-android/src/org/videolan/vlc/Media.java
+++ b/vlc-android/src/org/videolan/vlc/Media.java
@@ -164,10 +164,10 @@ public class Media implements Comparable<Media> {
         mFilename = file.getName().substring(0, file.getName().lastIndexOf('.'));
     }
 
-    public Media(Context context, String location, String filename, long time, long length, int type,
+    public Media(Context context, String location, long time, long length, int type,
             Bitmap picture, String title, String artist, String genre, String album) {
         mLocation = location;
-        mFilename = filename;
+        mFilename = null;
         mTime = time;
         mLength = length;
         mType = type;
@@ -196,6 +196,9 @@ public class Media implements Comparable<Media> {
     }
 
     public String getFileName() {
+        if (mFilename == null) {
+            mFilename = Util.URItoFileName(mLocation);
+        }
         return mFilename;
     }
 
@@ -240,7 +243,7 @@ public class Media implements Comparable<Media> {
         if (mTitle != null)
             return mTitle;
         else
-            return mFilename;
+            return getFileName();
     }
 
     public String getArtist() {
diff --git a/vlc-android/src/org/videolan/vlc/Util.java b/vlc-android/src/org/videolan/vlc/Util.java
index 19a10f0..9716ffd 100644
--- a/vlc-android/src/org/videolan/vlc/Util.java
+++ b/vlc-android/src/org/videolan/vlc/Util.java
@@ -45,6 +45,12 @@ public class Util {
         return new File(Uri.decode(URI).replace("file://",""));
     }
 
+    public static String URItoFileName(String URI) {
+        int sep = URI.lastIndexOf('/');
+        int dot = URI.lastIndexOf('.');
+        return Uri.decode(URI.substring(sep + 1, dot));
+    }
+
     public static String PathToURI(String path) {
         String URI;
         try {



More information about the Android mailing list