Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

java - Images not showing in gallery

I am working on the Whats'up status saver app. The status images are showing in the file manager but are not showing in the gallery.

Here is my code

public void downlaodImage(StatusModel statusModel) throws IOException {

        File file = new File(MyContanst.APP_DIRECTORY);
        if (!file.exists()){
            file.mkdirs();
        }
        File destFile = new File(file+ File.separator+statusModel.getTitle());
        if (destFile.exists()){
            destFile.delete();
        }

        copyFile(statusModel.getPath(), destFile);
        Toast.makeText(getActivity(), "File saved", Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(destFile));
        getActivity().sendBroadcast(intent);


    }

    private void copyFile(String file, File destFile) throws IOException {
        if (!destFile.getParentFile().exists()){
            destFile.getParentFile().mkdirs();
        }

        if (!destFile.exists()){
            destFile.createNewFile();
        }

        FileChannel src = null;
        FileChannel dst = null;

        src = new FileInputStream(file).getChannel();
        dst = new FileOutputStream(destFile).getChannel();
        dst.transferFrom(src, 0, src.size());

         src.close();
         dst.close();
    }

What I doing wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to call a broadcast to tell Android OS that you have added an Image or Video

So after download image/video just call below method will show your image/video in to gallery.

public  void scanMedia(File f, String type) {
        if (SDK_INT >= 19) {
            if (type.equals("video/*")) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Video.Media.DATA, f.getAbsolutePath());
                values.put(MediaStore.Video.Media.MIME_TYPE, type);
                values.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis());
                try{
                    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                    retriever.setDataSource(context, Uri.parse(f.getAbsolutePath()));
                    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                    long timeInMillisec = Long.parseLong(time);
                    values.put(MediaStore.Video.Media.DURATION, timeInMillisec);
                    retriever.release();
                }catch (Exception e){
                    values.put(MediaStore.Video.Media.DURATION, 0);
                }

                context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);

            }else if(type.equals("image/*")){
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, f.getAbsolutePath());
                values.put(MediaStore.Images.Media.MIME_TYPE, type);
                values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
                context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            }

        } else {
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(f)));
        }
    }

Happy Coding.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...