After saved image can't see it in Gallery android [duplicate]

若如初见. 提交于 2019-12-11 14:16:28

问题


I am saving Image from res/drawable to Gallery using InputStream and OutputStream. It works fine and save image. But issue is that It does not update Image in Gallery. If I check folder using ES File Explorer then I can see image there. I also checked ddms, It also update image there as soon as write code executes.

It works fine if I save Server Image. How to prevent this issue ? I want to update Gallery as soon as Image save.

I also tried MediaScanner to scan folder but no effect.

My Code:

Toast.makeText(context, "Downloading Image...\nPlease Wait.",
                Toast.LENGTH_LONG).show();

        File direct = new File(Environment.getExternalStorageDirectory()
                + "/Images");

        if (!direct.exists()) {
            direct.mkdirs();
        }

        DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy-HHmmss");
        Date date = new Date();
        String CurrentDateTime = dateFormat.format(date);

        InputStream input = null;
        OutputStream output = null;

        try {
            input = context.getResources().openRawResource(
                    context.getResources().getIdentifier(
                            "@drawable/" + picName, "drawable",
                            context.getPackageName()));
            output = new FileOutputStream(direct + "/" + "IMG-"
                    + CurrentDateTime + ".jpg");

            byte[] buf = new byte[1024];
            int len;
            while ((len = input.read(buf)) > 0) {
                output.write(buf, 0, len);
            }

            MediaScannerConnection.scanFile(context,
                    new String[] { direct.toString() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });

            Toast.makeText(context, "Image Saved.", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Log.e("Internal Image Save Error->", e.toString());

            Toast.makeText(context,
                    "Couldn't Save Image.\nError:" + e.toString() + "",
                    Toast.LENGTH_LONG).show();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
                if (output != null) {
                    output.close();
                }
            } catch (IOException ignored) {
                Log.e("Internal Image Save Error->", ignored.toString());

                Toast.makeText(
                        context,
                        "Couldn't Save Image.\nError:" + ignored.toString()
                                + "", Toast.LENGTH_LONG).show();
            }
        }

回答1:


you must broadcast external directory...

sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));

if you create external folder in sdcard and then its not display in gallary then use below code.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            Runtime.getRuntime().exec(
                    "am broadcast -a android.intent.action.MEDIA_MOUNTED -d file://"
                            + CommonVariable.abc_FOLDER);
        } else {
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                    Uri.parse("file://" + CommonVariable.abc_FOLDER)));
        }

other method for scanning.

Uri contentUri = Uri.fromFile(destFile);
        Intent mediaScanIntent = new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        mediaScanIntent.setData(contentUri);
        sendBroadcast(mediaScanIntent);

i hope its useful to you.



来源:https://stackoverflow.com/questions/25780945/after-saved-image-cant-see-it-in-gallery-android

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