Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图片未在库中显示_Java_Android - Fatal编程技术网

Java 图片未在库中显示

Java 图片未在库中显示,java,android,Java,Android,我正在开发Whats'up状态保护程序。状态图像显示在文件管理器中,但不显示在库中 这是我的密码 public void downlaodImage(StatusModel statusModel) throws IOException { File file = new File(MyContanst.APP_DIRECTORY); if (!file.exists()){ file.mkdirs(); }

我正在开发Whats'up状态保护程序。状态图像显示在文件管理器中,但不显示在库中

这是我的密码

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();
    }

我做错了什么?

您需要呼叫广播,告诉Android操作系统您添加了图像或视频

所以,下载完图像/视频后,只需调用下面的方法,即可将您的图像/视频显示在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)));
        }
    }

愉快的编码。

您拯救了我的一天,非常感谢您……这对Android 10+不起作用。您没有检查mkdirs()和delete()的返回值。因此,您没有按照这些值进行操作。此外,您不必调用createNewFile(),因为FileOutputStream将创建该文件。删除该声明。这里也没有检查返回值,所以只有三件事做错了;-)。