保存后图像未显示在图库中

时间:2019-01-16 11:34:30

标签: android file android-gallery android-mediascanner

嗨,我正在尝试将图片保存在我的图片库中,但问题是,无法在我的图片库中看到,以下是我的代码能帮上什么忙吗?

public void OnClickSave(View view)
    {
        Bitmap bitmap =getBitmapFromView(idForSaveView);

        try {


            ContextWrapper wrapper = new ContextWrapper(context);

            File file = wrapper.getDir("MilMilaImages",MODE_PRIVATE);

            // Create a file to save the image
            file = new File(file, "MilMila"+".jpg");

            try{

                OutputStream stream = null;

                stream = new FileOutputStream(file);


                bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);


                stream.flush();


                stream.close();

            }catch (IOException e) // Catch the exception
            {
                e.printStackTrace();
            }

            // Parse the gallery image url to uri
            final Uri savedImageURI = Uri.parse(file.getAbsolutePath());

            // Display the saved image to ImageView
            System.out.println("HLL"+savedImageURI);

            iv.setImageURI(savedImageURI);



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

                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                Intent mediaScanIntent = new Intent(
                        Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri contentUri = Uri.fromFile(file);
                mediaScanIntent.setData(contentUri);
                context.sendBroadcast(mediaScanIntent);
            } else {
                context.sendBroadcast(new Intent(
                        Intent.ACTION_MEDIA_MOUNTED,
                        Uri.parse("file://"
                                + Environment.getRootDirectory())));
            }

            // Display saved image uri to TextView

//            Toast.makeText(context,"Saved Successfully",Toast.LENGTH_LONG).show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:0)

我已经使用了这段代码,并且对我有用:

// show the image in the device gallery
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
        val contentUri = Uri.fromFile(compressFile) //out is your output file
        mediaScanIntent.data = contentUri
        this.sendBroadcast(mediaScanIntent)
    } else {
        sendBroadcast(Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())))
    }// // show the image in the device gallery