Reputation: 4286
I want to display all the videos present in the phone storage (internal or SD card) inside my app's listview and my old code is giving exceptions on new 7.0 devices. My targetSDK API level is 23. I also get a warning in the IDE that "managedQuery" method is deprecated. The method that I have in my project is:
private boolean getVideoList() {
Cursor var1 = this.managedQuery(Media.EXTERNAL_CONTENT_URI, new String[]{"_data", "_id", "_display_name", "duration"}, (String)null, (String[])null, " _id DESC");
int var2 = var1.getCount();
if(var2 <= 0) {
return false;
} else {
var1.moveToFirst();
for(int var4 = 0; var4 < var2; ++var4) {
Uri var5 = Uri.withAppendedPath(Media.EXTERNAL_CONTENT_URI, ContentUtill.getLong(var1));
String var6 = var1.getString(var1.getColumnIndexOrThrow("_display_name"));
String var7 = var1.getString(var1.getColumnIndex("_data"));
String var8 = ContentUtill.getTime(var1, "duration");
VideoData var9 = new VideoData(var6, var5, var7, var8);
this.videoList.add(var9);
var1.moveToNext();
}
return true;
}
}
The exception is:
Caused by: java.lang.SecurityException:
at android.os.Parcel.readException (Parcel.java:1693)
at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query (ContentProviderNative.java:421)
at android.content.ContentResolver.query (ContentResolver.java:536)
at android.content.ContentResolver.query (ContentResolver.java:478)
at android.app.Activity.managedQuery (Activity.java:2368)
at video.format.converter.view.SelectVideoActivity.getVideoList (SelectVideoActivity.java:61)
at video.format.converter.view.SelectVideoActivity.access$0 (SelectVideoActivity.java:60)
at video.format.converter.view.SelectVideoActivity$loadVideo.doInBackground (SelectVideoActivity.java:170)
at video.format.converter.view.SelectVideoActivity$loadVideo.doInBackground (SelectVideoActivity.java:1)
at android.os.AsyncTask$2.call (AsyncTask.java:304)
at java.util.concurrent.FutureTask.run (FutureTask.java:237)
I do have the new permission model implemented in my project and I do ask for WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions but still the exception. Any good way to get a list of videos and populate my POJO class with properties:
public String Duration;
public Uri VideoUri;
public long videoId;
public String videoName;
public String videoPath;
Upvotes: 0
Views: 962
Reputation: 227
I think you are working with Android Marshmallow or above. You need to get runtime permission for READ_EXTERNAL_STORAGE.Just putting it in manifest will not work for Android 6+. Here is a link to add runtime permission.Link
Also managed query is deprecated, query like this:
Cursor var1 = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]{"_data", "_id", "_display_name", "duration"}, (String)null, (String[])null, " _id DESC");
Upvotes: 1
Reputation: 420
void findVideos(File dir, ArrayList<String> list){
for (File file : dir.listFiles()) {
if (file.isDirectory())
findVideos(file, list);
else if(file.getAbsolutePath().contains(".mp4"))
list.add(file.getAbsolutePath());
}
}
Upvotes: 0