简体   繁体   中英

Open image from directory not in gallery

I want to open up all images within a folder that I created but not located in the gallery. So far, I've searched and have somewhat got it to work. The only problem is that it just displays the image gallery showing me which folder to pick. Its like it ignores the path and defaults to the gallery. Is there something wrong with what I'm doing? Please be gentle, I usually search and search more, but maybe not enough? Below is a snippet of what I'm working with.

Also, I've tried the solution here: Built-in gallery in specific folder , but it just shows one image, not all images within the directory. Maybe I need to get back to this one and alter it to work?

private void setIntentToSpecificFolder() {
    String folderPath = Environment.getExternalStorageDirectory().toString() + "/com.qk.livewallpapertest/";
    //int folderBucketId = folderPath.toLowerCase().hashCode();
    //Uri targetUri = Media.EXTERNAL_CONTENT_URI.buildUpon().appendQueryParameter("bucketId", String.valueOf(folderBucketId)).build();
    Uri targetUri = Uri.parse(folderPath);

    Log.v(TAG,"targetUri: " + getRealPathFromURI(targetUri) + "folderPath: " + folderPath);

    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setDataAndType(targetUri, "vnd.android.cursor.dir/image");
    startActivityForResult(intent,SELECT_PHOTO);
}

Whenever you use an action like ACTION_PICK , you are implicitly handing control to another app, just as a link to a third-party Web site hands control to another Web site.

In this case, there is no requirement for a device to have anything that can do ACTION_PICK from a particular directory. A device may have zero, one, or N apps that are capable of doing that. What those apps are, and what they do, is up to their respective authors.

Hence, if the functionality of your app depends upon the ability for the user to pick an image out of a specific directory, you need to implement that yourself. If this is a nice-to-have, but not essential, you could stick with ACTION_PICK but suggest that users install such-and-so file explorer app that you determine delivers the results that you want.

Here is a simplified one

private  MediaScannerConnection conn;
private void notifySystemWithImage(final File imageFile) {

        conn = new MediaScannerConnection(this, new MediaScannerConnectionClient() {

        @Override
        public void onScanCompleted(String path, Uri uri) {

            try {
                if (uri != null) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(uri, "image/*");
                    startActivity(intent);
                }
            } finally {
                conn.disconnect();
                conn = null;
            }
        }

        @Override
        public void onMediaScannerConnected() {
            conn.scanFile(imageFile.getAbsolutePath(), "*/*"); 

        }
    });

    conn.connect();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM