How can I refresh the Gallery after I inserted an Image in android?

后端 未结 5 1546
旧时难觅i
旧时难觅i 2020-12-01 06:47

I\'ve added an inserted in Gallery using android API as following:

Images.Media.insertImage(ctx.getContentResolver(), \"scard/test.jpg\", \"Hello\"

5条回答
  •  不知归路
    2020-12-01 07:28

    i tied everything then I found this perfect solution! You can apply this method for any file type (jpg, png,pdf, etc)

    public void refreshGallery(File f) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Intent mediaScanIntent = new Intent(
                    Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri fileUri = Uri.fromFile(f); //out is your output file
            mediaScanIntent.setData(fileUri);
            sendBroadcast(mediaScanIntent);
        } else {
            sendBroadcast(new Intent(
                    Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        }
    }
        
    

    then

    refreshGallery(newFileName);
    

    newFIleName is a file path you can get your file path as

    File newFileName = new File(filePath + "/" + fileName + ".jpg");
    

提交回复
热议问题