Reputation: 527
I want to open all the pictures from a specific folder using the Android Gallery. I found a solution in an old question but it didn't work. The code is:
public class MainActivity extends Activity implements MediaScannerConnectionClient {
public String[] allFiles;
private String SCAN_PATH;
private static final String FILE_TYPE = "image/*";
private MediaScannerConnection conn;
private String folderName = "MyAPP";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
File folder = new File("/sdcard/"+folderName+"/");
allFiles = folder.list();
SCAN_PATH = Environment.getExternalStorageDirectory().toString()
+ "/"+folderName+"/" + allFiles[0];
Button scanBtn = (Button) findViewById(R.id.button1);
scanBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startScan();
}
});
}
private void startScan() {
if (conn != null) {
conn.disconnect();
}
conn = new MediaScannerConnection(this, this);
conn.connect();
}
public void onMediaScannerConnected() {
conn.scanFile(SCAN_PATH, FILE_TYPE);
}
public void onScanCompleted(String path, Uri uri) {
try {
if (uri != null) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setData(uri);
startActivity(intent);
}
} finally {
conn.disconnect();
conn = null;
}
}
}
With this code I'm getting this error:
08-14 11:09:20.093: W/ContentResolver(25151): Failed to get type for: content://media/external/images/media/10471 (Unknown URL : content://media/external/images/media/10471)
08-14 11:09:20.093: E/JavaBinder(25151): *** Uncaught remote exception! (Exceptions are not yet supported across processes.)
08-14 11:09:20.093: E/JavaBinder(25151): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT dat=content://media/external/images/media/10471 }
08-14 11:09:20.093: E/JavaBinder(25151): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
08-14 11:09:20.093: E/JavaBinder(25151): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
08-14 11:09:20.093: E/JavaBinder(25151): at android.app.Activity.startActivityForResult(Activity.java:3351)
08-14 11:09:20.093: E/JavaBinder(25151): at android.app.Activity.startActivityForResult(Activity.java:3312)
08-14 11:09:20.093: E/JavaBinder(25151): at android.app.Activity.startActivity(Activity.java:3522)
08-14 11:09:20.093: E/JavaBinder(25151): at android.app.Activity.startActivity(Activity.java:3490)
08-14 11:09:20.093: E/JavaBinder(25151): at com.example.camtest.MainActivity.onScanCompleted(MainActivity.java:64)
08-14 11:09:20.093: E/JavaBinder(25151): at android.media.MediaScannerConnection$1.scanCompleted(MediaScannerConnection.java:53)
08-14 11:09:20.093: E/JavaBinder(25151): at android.media.IMediaScannerListener$Stub.onTransact(IMediaScannerListener.java:60)
08-14 11:09:20.093: E/JavaBinder(25151): at android.os.Binder.execTransact(Binder.java:367)
08-14 11:09:20.093: E/JavaBinder(25151): at dalvik.system.NativeStart.run(Native Method)
It'd be ok if I can get this code works but I can use any other idea you suggest me
Upvotes: 0
Views: 6548
Reputation: 578
I have retrieved images path from a specific folder and displayed it on recycler View. The code is in Kotlin but you can get a idea from
private fun getAllShownImagesPath(): ArrayList<String> {
var filePath: ArrayList<String> = ArrayList<String>()
val path = File(getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).path, "BacFo Camera")
if (path.exists()) {
for (i in path.list().iterator()) {
filePath?.add("" + path.toString() + "/" + i)
}
}
return filePath!!
}
So, now you have list of images path that you can pass on to recycler view using recycler view Adapter. Easy :-)
Upvotes: 0
Reputation: 606
Here's the actual correct solution:
The secret is finding the bucket/album your folder is represented as. Buckets show up after a successful MediaScan so be sure any images/videos you want to show are first scanned as demonstrated multiple times above.
Let's assume I have an indexed folder in /sdcard/myapp/myappsmediafolder:
String bucketId = "";
final String[] projection = new String[] {"DISTINCT " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME + ", " + MediaStore.Images.Media.BUCKET_ID};
final Cursor cur = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
while (cur != null && cur.moveToNext()) {
final String bucketName = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME)));
if (bucketName.equals("myappsmediafolder")) {
bucketId = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID)));
break;
}
}
Now that we have the bucketId for our album we can open it with a simple intent.
Filters Video files:
Uri mediaUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
Filters Image files:
Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
...
if (bucketId.length() > 0) {
mediaUri = mediaUri.buildUpon()
.authority("media")
.appendQueryParameter("bucketId", bucketId)
.build();
}
Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
startActivity(intent);
I can verify this works with the built-in Gallery app. Mileage may vary with other apps such as Google Photos.
I have yet to figure out how not to filter images/video, even though within Gallery you can select a specific Album with no filter.
I figured this out by looking at the AOSP source to the gallery app.
This questions is also a duplicate of: Gallery with folder filter
Upvotes: 1
Reputation: 391
Try this on button click
browse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
and you can set this selected image into your application as follows
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
imageView = (ImageView) findViewById(R.id.property_image);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}}
In first block of code i use startActivityForResult(i, RESULT_LOAD_IMAGE); this return result to called activity and we can get this result by second block of code, and set selected image in your ImageView
Upvotes: 0
Reputation: 350
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setData(uri);
startActivity(intent);
The code you use above is to open picture in gallery, but not passing a uri to gallery to open it. If you want to view pictures in a folder by gallery, I introduce you to see How to open gallery to show images in a specific directory ,hope it will help.
Upvotes: 1