i am trying to populate listview with video files from a folder created on sd card. I am using
managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,mystring, null, null, null);
But it populates all videos saved in sdcard, but i want only those videos which are saved in specific folder. I have also used
Uri uri = Uri.fromFile(filepath);
cursor = managedQuery(uri, mystring , null , null , null);
and
Uri uri = Uri.parse(filepath);
cursor = managedQuery(uri, mystring , null , null , null);
But it doesn't work. I have tried lot and got help from google still not succeded.
Is there any way to give path of that folder? or any other way?
you can use this code for get videos from specific folder as:
String selection=MediaStore.Video.Media.DATA +" like?";
String[] selectionArgs=new String[]{"%FolderName%"};
videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
parameters, selection, selectionArgs, MediaStore.Video.Media.DATE_TAKEN + " DESC");
You can get a files form a specific location like this
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File("/sdcard/");
// or you can use File f=new File(Environment.getExternalStorageDirectory().getPAth());
File[] files = f.listFiles();
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
hey managedquery search the whole sdcard not the specific folder .
the above method is true of display the name of file but if you to display thumbnail create thumbmail and store in in Hashmap and populate it to the list of gallery adapter..........
Related
I have developed a android application which plays music on my phone.
I have created the following directory structure on my phone.
data
Music
Engish
Song 1
Song 2
Jazz
Song 1
Song 2
Code Snippet
To get the list of Music categories (returns the folder names)
filepath = new File(Environment.getExternalStorageDirectory().getAbsoluteFile().getPath() + "/data/Music/");
String[] directories = filepath.list(new FilenameFilter()
The list of categories gets populated on the phone
English
Jazz
When the user selects a category (eg: English), the list of songs get populated for that category.
To get the list of songs under a Category
File filepath = new File(Environment.getExternalStorageDirectory()
.getAbsoluteFile().getPath() + "/data/Music/");
folder = filepath.toString() + "/" + categoryName;
folder value = /storage/emulated/0/data/Music/English
folder = folder + "/%";
**folder value = /storage/emulated/0/data/Music/English/%**
String where = MediaStore.Audio.Media.DATA + " like ? ";
String[] whereArgs = new String[] { folder };
String[] col = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.SIZE };
musiccursor = getActivity().getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, col, where,
whereArgs,
"UPPER(" + MediaStore.Audio.Media.DISPLAY_NAME + ") ASC");
musiclist = (ListView) getView().findViewById(R.id.MusicList);
musiclist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
musiclist.setAdapter(new MusicAdapter(getActivity(),R.layout.song_list_item, musiccursor));
musiclist.setOnItemClickListener(musicgridlistener);
The above code works on my phone.
Now I want to move the songs to a SD (Storage) Card on my phone.
For that I had to create the following directory structure
On the card, there is already a folder Android/data.
Within that I had to create a folder called com.android.gmp/files/data (gmp is my package name).
So the directory struture is:
Android
data
com.android.gmp
files
data
Music
Engish
Song 1
Song 2
Jazz
Song 1
Song 2
The code to get the folders from the card
File[] storages = ContextCompat.getExternalFilesDirs(getActivity(), null);
if (storages.length > 1 && storages[0] != null && storages[1] != null) {
File file = storages[1];
String path = file.getAbsolutePath();
filepath = new File(path + "/data/Music/");
String[] directories = filepath.list(new FilenameFilter();
}
filepath value = /storage/3862-3539/Android/data/com.android.gmp/files/data/Music
The list of categories gets populated on the phone
English
Jazz
To get the songs under the category (No songs are returned - this is the issue)
folder = filepath.toString() + "/" + categoryName;
folder value = /storage/3862-3539/Android/data/com.android.gmp/files/data/Music/English/%
Same code as above
String where = MediaStore.Audio.Media.DATA + " like ? ";
String[] whereArgs = new String[] { folder };
String[] col = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.SIZE };
musiccursor = getActivity().getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, col, where,
whereArgs,
"UPPER(" + MediaStore.Audio.Media.DISPLAY_NAME + ") ASC");
musiclist = (ListView) getView().findViewById(R.id.MusicList);
musiclist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
musiclist.setAdapter(new MusicAdapter(getActivity(),R.layout.song_list_item, musiccursor));
musiclist.setOnItemClickListener(musicgridlistener);
This query does not work, does not get the songs under the folder - if anyone can let me know where the issue is.
There are lots of other classes in the application but this issue is specifically getting files under a folder on a SD card
I was able to find a solution for my issue.
I had to get a list of songs from a external storage (SD card)and populate a ListView using a cursor.
I already have code which was populating the songs from internal storage so did not want to make too many changes.
Below is the code snippet
Step 1. Create a new Content Provider (SongContentProvider inherited from ContentProvider)
`public class SongContentProvider extends ContentProvider
Overwrite the query method
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
//selection contains folder path (in my case = /storage/3862-3539/Android/data/com.android.gmp/files/data/Music/English
String[] matrixColumns = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.SIZE };
MatrixCursor songListCursor = new MatrixCursor(matrixColumns);
File directory = null;
File[] fileList = null;
directory = new File(selection);
fileList = directory.listFiles();
Arrays.sort(fileList);
Object[] mRow = new Object[4];
for(int i=0;i<fileList.length;i++){
mRow[0] = i;
mRow[1] = fileList[i];
mRow[2] = fileList[i].getName();
mRow[3] = "";
songListCursor.addRow(mRow);
}
return songListCursor;
}
`
Step 2. Call query from PlaySongFragment.java
private final Uri songContentProvider =
Uri.parse("content://com.android.gmp.songs/songs");
Cursor musiccursor = getActivity().getContentResolver().query(songContentProvider, null, folderPath, null, null);
musiclist.setAdapter(new MusicAdapter(getActivity(),R.layout.song_list_item, musiccursor));
Step 3. Add content provider to the manifest.xml
<provider
android:authorities="com.android.gmp.songs"
android:name=".SongContentProvider">
</provider>
I am trying to make a simple gallery from an asset folder in my app.
so I created an asset folder, and created inside a directory called 'pics' and put some jpg files inside.
Then I did this to get all the images from the pics inside the assets folder. I wanted to get it by the URI of the assets folder but it's not working, I guess because the URI is wrong:
Uri uriExternal = Uri.parse(getApplicationContext().getResources().getAssets().open("pics").toString());
String[] projection = { MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.MediaColumns.DATE_MODIFIED };
Cursor cursorExternal = getContentResolver().query(uriExternal, projection, "bucket_display_name = \""+album_name+"\"", null, null);
Cursor cursor = new MergeCursor(new Cursor[]{cursorExternal});
while (cursor.moveToNext()) {
path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
imageList.add(Function.mappingInbox(album, path, null));
}
Use something like this:
AssetManager assets = getApplicationContext().getResources().getAssets();
String[] pics = assets.list("pics");
ArrayList<Bitmap> bmps = new ArrayList<>();
for (String path : pics) {
bmps.add(BitmapFactory.decodeStream(assets.open(path));
}
The bmps list now contains all the images in your pics folder as Bitmaps.
I wrote this purely from documentation, and I haven't tested it. I recommend you read the documentation yourself if you run into trouble: https://developer.android.com/reference/android/content/res/AssetManager
This question already has answers here:
Simple mediaplayer play mp3 from file path?
(7 answers)
Closed 5 years ago.
I have been building simple mp3 player everything is going well the code which fetches all mp3 files from storage device's works well.
Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
My question i need to load mp3 files only from sdcard kalid folder. i have tried this code but its not loading mp3 files from kalid folder.
File path = new File(Environment.getExternalStorageDirectory() + "/kalid");
Uri media = Uri.fromFile(path);
i have also tired this code
File path = new File(Environment.getExternalStorageDirectory() + "/kalid");
Uri media = MediaStore.Audio.Media.getContentUriForPath(String.valueOf(path));
I think no one is understanding my question here is full source code
ContentResolver resolver = context.getContentResolver();
Uri media = MediaStore.Audio.Media.getContentUriForPath("Environment.getExternalStorageDirectory().getPath()+ kalid+");
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
Cursor cursor = resolver.query(media, PROJECTION, selection, null, null);
if (cursor == null || cursor.getCount() == 0) {
Log.d(TAG, "Music List Empty");
return false;
}
mMusicList.clear(); // clear
int count = cursor.getCount();
for (int i = 0; i != count; ++i) {
if (!cursor.moveToNext()) {
break;
}
//Log.d(TAG, cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
// Add to ArrayList
mMusicList.add(new Music(
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))
));
}
cursor.close();
Finally after trying so many things i have listed mp3 files from kalid folder here is the source code.
Cursor cursor = resolver.query(media,
PROJECTION,
MediaStore.Audio.Media.DATA + " like ? ",
new String[]{"%kalid%"}, null);
I'm trying to read photos existing in the sdcard with MediaStore.Images.Media.DATA but I'm always getting an empty cursor !!!
This is the code I'm using for the reading :
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
Cursor imagecursor = getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
MediaStore.Images.Media.DATA + " like ? ",
new String[] { "%"+eventName.trim()+"%" }, null);
imagecursor.setNotificationUri(getContentResolver(),
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
this.imageUrls = new ArrayList<String>();
Log.i("imagecursor.getCount()", Integer.toString(imagecursor.getCount()));
for (int i = 0; i < imagecursor.getCount(); i++) {
imagecursor.moveToPosition(i);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
imageUrls.add(imagecursor.getString(dataColumnIndex));
}
Of course I added this
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
to the AndroidManifest.xml
I found that the problem was with the Media Scanner so i solved it buy using a simple app called Scan Media witch scan the external storage and then the MediaStore will be able to find the new elements .
i donĀ“t know if this will help you but I is working me
String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/yoururl/";
Toast.makeText(getActivity(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files){
myImageAdapter.add(file.getAbsolutePath());
}
in this example i get images and show in a grid view
I can get the list of images in sd card using fololowing`
but these are not sorted
how can I get them sorted either by name or time
thanks in advance
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory().getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/somedirectoryname/";
File targetDirectory = new File(targetPath);
File[] files = targetDirectory.listFiles();`
for (File file : files1) {
list.add(file.getAbsolutePath().toString());
}
Maybe it will be easier if you use the built-in content provider
MediaStore.Images.Media.query(cr, uri, projection, where, orderBy)
something like this:
String[] projection = {MediaColumns._ID, MediaColumns.DISPLAY_NAME, MediaColumns.DATE_ADDED};
Cursor c = MediaStore.Images.Media.query(getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, MediaColumns.DATE_ADDED);
then you can loop the cursor to do whatever you need.
- Use lastModified() method of File to get the time the file was modified.
- Store them in Collection like ArrayList<File>.
- Use java.util.Comparator<T> to compare and sort them according to time and name.