Изображение не отображается в галерее после сохранения - PullRequest
0 голосов
/ 16 января 2019

Привет. Я пытаюсь сохранить изображение в своей галерее, но проблема в том, что, если я не могу увидеть в моей галерее, может ли помочь мой код, следующий код?

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 голосов
/ 16 января 2019

Я использовал этот код, и он работает для меня:

// 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
...