java - 下载的文件在用户媒体应用程序中不可见?

标签 java android download

我需要使下载的文件(例如 mp3 图片等)在相册或媒体播放器等媒体应用程序中可见

它是文件 chmod 还是不可读?此类中的一切都很好,但它仅在下载文件夹中显示下载的文件

这是我的课

public class Download extends AsyncTask<String, Void, String> {
        ProgressDialog mProgressDialog;

        Context context;
        String file_url,file_name;

        public Download(Context context,String url , String file_name) {
        this.context = context;

        this.file_url  = url;
        this.file_name = file_name;

        }

        protected void onPreExecute() {
        mProgressDialog = ProgressDialog.show(context, "",
        "Please wait, Download …");
        }

        protected String doInBackground(String... params) {



        // //////////////////////
        try {

        URL url = new URL(file_url);
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();
        String[] path = url.getPath().split("/");
        String mp3 = path[path.length - 1];
        int lengthOfFile = c.getContentLength();

        String PATH = Environment.getExternalStorageDirectory()+ "/downloads/" ;

        File file = new File(PATH);
        file.mkdirs();

        String fileName = file_name;

        File outputFile = new File(file , fileName);
        FileOutputStream fos = new FileOutputStream(outputFile);

        InputStream is = c.getInputStream();

        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = is.read(buffer)) != -1) {

        fos.write(buffer, 0, len1);
        }
        fos.close();
        is.close();

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



        return "done";
        }

        protected void onPostExecute(String result) {
        if (result.equals("done")) {
        mProgressDialog.dismiss();

        }
}

}

有什么想法吗?

最佳答案

您需要调用 MediaScannerConnection 并告诉它扫描您的文件。文档 here .

关于java - 下载的文件在用户媒体应用程序中不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15776690/

相关文章:

Android - SQLite AUTOINCRMENT 不增加主键

android - 为什么更改布局参数的设置位置可以解决我的 "OnClickListener not working for first item in GridView"问题?

java - 如何以编程方式将 View 约束到 ConstraintLayout 中的父 View

java - Rest API 中更新方法的 URL,将对象作为查询参数发送给它

java - 如何使用页面工厂和 Selenium 来初始化函数

java - Android中如何不使用getLastKnownLocation方法获取当前经纬度?

java - GWT 下载 Excel .xlsx 给我一个损坏的文件

linux - 检查下载/上传是否正在进行

python - 通过 Post 表单下载文件

java - Azure 函数服务总线触发器的 Java 是否支持 BrokeredMessage?