How can I notify the gallery of a new image in and

2020-03-30 07:32发布

问题:

I have an android app that allows you to open an image on your phone using an intent to the Gallery. You make modifications to that image and the app saves it with a new name in the same folder the original was in. Since the gallery can't constantly be looking for new images is there any way to notify the gallery of a new image from my android app so it shows up next time the user opens the gallery?

回答1:

use the MediaScannerConnection -

MediaScannerConnection.scanFile(context,
    new String[] { imagePath }, null,
    new MediaScannerConnection.OnScanCompletedListener() {                      
        @Override
        public void onScanCompleted(String path, Uri uri) {
            //....                              
        }
    });


回答2:

As best I can tell from digging through the Android Camera app source, in the ActivityBase.java class there is an inner class called "MyAppBridge" which has the following comment:

//////////////////////////////////////////////////////////////////////////
//  The is the communication interface between the Camera Application and
//  the Gallery PhotoPage.
//////////////////////////////////////////////////////////////////////////

Within that class there is a function as follows:

    public void addSecureAlbumItem(boolean isVideo, int id) {
        if (mServer != null) mServer.addSecureAlbumItem(isVideo, id);
    }

Now the problem is trying to figure out where the Server class comes from, since searching the rest of the project doesn't turn anything up and I can't find it in the imports of the ActivityBase class.

I know this isn't a complete answer but it should hopefully point you in the right direction. When in doubt: Use the source, Luke.