How to Display Images from a Specific Folder on Android Gallery

How can I display images from a specific folder on android gallery

Hi you can use the code below, i hope it helps you .

package com.example.browsepicture;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BrowsePicture2 extends Activity {
String SCAN_PATH;
File[] allFiles ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse_picture);

File folder = new File(Environment.getExternalStorageDirectory().getPath()+"/aaaa/");
allFiles = folder.listFiles();

((Button) findViewById(R.id.button1))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new SingleMediaScanner(BrowsePicture2.this, allFiles[0]);
}
});
}

public class SingleMediaScanner implements MediaScannerConnectionClient {

private MediaScannerConnection mMs;
private File mFile;

public SingleMediaScanner(Context context, File f) {
mFile = f;
mMs = new MediaScannerConnection(context, this);
mMs.connect();
}

public void onMediaScannerConnected() {
mMs.scanFile(mFile.getAbsolutePath(), null);
}

public void onScanCompleted(String path, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
mMs.disconnect();
}

}
}

Android display images from a specific folder

The above code will only opens the folder. You have to use GallerView or GridView with the help of Adapter you can set images to ListView or GridView. You have to give correct path in-order to open the folder you want.
GridView loading photos from SD Card - refer this link

Android Display Images From Specific Folder in Gallery View

Figured it out! Below is the pasted code for anyone who wants to do something similar.

Cursor imagecursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
columns,
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%/yourfoldername/%"},
null);

How to list images from a specific folder

Try using the following function for the same :
First declare a const variable with the name of the folder that you want to search for :

const val TESTAPP = "TestApp"

And then use the following function to get data

override suspend fun getData(): List<UserWork>? =

withContext(Dispatchers.IO) {
try {

val collection = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
} ?: MediaStore.Images.Media.EXTERNAL_CONTENT_URI

val selection =
MediaStore.Images.ImageColumns.RELATIVE_PATH + " like ? "
val projection = arrayOf(
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.RELATIVE_PATH,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.BUCKET_ID,
MediaStore.MediaColumns.WIDTH
)

val selectionArgs = arrayOf("%$TESTAPP%")

val sortOrder = MediaStore.MediaColumns.DATE_ADDED + " COLLATE NOCASE DESC"

val itemList: MutableList<UserWork> = mutableListOf()

context.contentResolver?.query(
collection,
projection,
selection,
selectionArgs,
sortOrder
)?.use { cursor ->

val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
val displayNameColumn =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)
val relativePathColumn =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.RELATIVE_PATH)
val widthPathColumn =
cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.WIDTH)

while (cursor.moveToNext()) {
val id = cursor.getLong(idColumn)
val displayName = cursor.getString(displayNameColumn)
val relativePath = cursor.getString(relativePathColumn)
val width = cursor.getInt(widthPathColumn)

val contentUri = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
id
)

itemList.add(UserWork(id, displayName, contentUri))

}
cursor.close()
}

itemList
} catch (e: Exception) {
Log.d(
"MediaStoreException", "The exception for getData is " +
"$e"
)
null
}

}

How to read Image from a specific folder on Android 10 using MediaStore

I did a projection on RELATIVE_PATH constant instead of BUCKET_DISPLAY_NAME (which just gives you the name of last folder) and a selection of LIKE DCIM/Test% using the below code to filter out the results from DCIM/Test folder.

val projection = arrayOf(_ID, DATE_ADDED, BUCKET_DISPLAY_NAME, RELATIVE_PATH)
val selection = "${MediaStore.MediaColumns.RELATIVE_PATH} LIKE ?"
val selectionArgs = arrayOf("DCIM/Test%") // Test was my folder name
val sortOrder = "$DATE_ADDED DESC"

app.contentResolver.query(
EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
)?.use {
val id = it.getColumnIndexOrThrow(_ID)
val bucket = it.getColumnIndexOrThrow(BUCKET_DISPLAY_NAME)
val date = it.getColumnIndexOrThrow(DATE_ADDED)
val path = it.getColumnIndexOrThrow(RELATIVE_PATH)
while (it.moveToNext()) {
// Iterate the cursor
}
}

The only disadvantage of this approach is that RELATIVE_PATH is available for API level 29 (Q) and above.

Edit: Changed the selection args from %DCIM/Test% to DCIM/Test% as pointed out by Alejandro Gomez. The prior will match to any DCIM folder (including the system default) eg. My/DCIM/Test/Sample.jpg will also be in the output alongside DCIM/Test/Sample.jpg.

How to show last taken image from specific folder?

To get the latest modified file in folder for specific extension

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.WildcardFileFilter;

...

/* Get the newest file for a specific extension */
public File getTheNewestFile(String filePath, String ext) {
File theNewestFile = null;
File dir = new File(filePath);
FileFilter fileFilter = new WildcardFileFilter("*." + ext);
File[] files = dir.listFiles(fileFilter);

if (files.length > 0) {
/** The newest file comes first **/
Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
theNewestFile = files[0];
}

return theNewestFile;
}

Addition

To get all files or only png and jpeg use

new WildcardFileFilter("*.*");
new WildcardFileFilter(new String[]{"*.png", "*.jpg"});


Related Topics



Leave a reply



Submit