Android open gallery from folder

▼魔方 西西 提交于 2021-02-09 02:47:51

问题


I want to show a photo in android gallery, and be able to slide throw the others photos on that folder.

Intent intent = new Intent(Intent.ACTION_VIEW);
File f = new File(path);
intent.setDataAndType(Uri.parse("file://" + f.getAbsolutePath()), "image/*");
mContext.startActivity(intent);

thats how i am doing it now, but wont let me slide throw the rest of the images in the folder. i tried:

How to open one particular folder from gallery in android?

Built-in gallery in specific folder

Gallery with folder filter

Without any luck. i would be really happy if someone have the solution. Thanks!


回答1:


Try This

Intent i=new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(path)), "image/*");  
startActivity(i);

See these Links

How can I use Intent.ACTION_VIEW to view the contents of a folder?

Android ACTION_VIEW Multiple Images

Java and Android: How to open several files with an Intent?

if this solves your problem. Also check

https://www.google.co.in/?gfe_rd=cr&ei=c5n9U6ruE7DO8gfXz4G4BA&gws_rd=ssl#q=view+like+gallery

also check Gallery widget




回答2:


Try this way,hope this will help you to solve your problem.

final int OPEN_GALLERY = 1
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), OPEN_GALLERY);



回答3:


This question is from five years ago, However I want to give an answer that worked for me instead of the correct answer.

In order to show the photo and slide through the others photos, we need to give to the intent not the file uri but the media uri.

public void ShowPhoto(File imageFile) {

    String mediaId = "";

    String[] projection = new String[] {
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DISPLAY_NAME
    };

    Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection, null, null, null);

    while (cursor != null && cursor.moveToNext()) {
        String name = cursor.getString((cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME)));
        if(name.equals(imageFile.getName())){
            mediaId = cursor.getString((cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)));
            break;
        }
    }

    Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    if(!mediaId.equals("")){
        mediaUri = mediaUri.buildUpon()
                .authority("media")
                .appendPath(mediaId)
                .build();
    }

    Log.d("TagInfo","Uri:  "+mediaUri);

    Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
    startActivity(intent);

}


来源:https://stackoverflow.com/questions/25478828/android-open-gallery-from-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!