下载的图像在android studio中显示,但不在gallery中显示

下载的图像在android studio中显示,但不在gallery中显示,android,android-file,android-download-manager,Android,Android File,Android Download Manager,我正在尝试通过我创建的服务使用url下载图像,当我通过Android Studio调试器检查设备的文件目录时,我可以在设备中看到文件: 红色文件是有问题的下载文件。绿色图像文件是我通过Windows资源管理器手动拖动到目录中的文件。 我可以在我的Gallery应用程序中成功查看并打开绿色文件,但红色文件(以及该目录中列出的所有其他文件)在哪里都找不到。我也无法在Windows资源管理器中看到它们。我是否应该在代码中的某个地方将它们标识为图像,以便文件系统知道它是图像 这是我下载图像的部分: R

我正在尝试通过我创建的服务使用url下载图像,当我通过Android Studio调试器检查设备的文件目录时,我可以在设备中看到文件:

红色文件是有问题的下载文件。绿色图像文件是我通过Windows资源管理器手动拖动到目录中的文件。

我可以在我的Gallery应用程序中成功查看并打开绿色文件,但红色文件(以及该目录中列出的所有其他文件)在哪里都找不到。我也无法在Windows资源管理器中看到它们。我是否应该在代码中的某个地方将它们标识为图像,以便文件系统知道它是图像

这是我下载图像的部分:

Request.Builder builder = new Request.Builder();
builder = builder.url(downloadFileUrl);
builder = builder.addHeader("RANGE", "bytes=" + existLocalFileLength);
Request request = builder.build();

Call call = okHttpClient.newCall(request);
Response response = call.execute();

if (response != null && response.isSuccessful()) {
 RandomAccessFile downloadFile = new RandomAccessFile(existLocalFile, "rw");
 downloadFile.seek(existLocalFileLength);

 ResponseBody responseBody = response.body();
 InputStream inputStream = responseBody.byteStream();
 BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
 byte data[] = new byte[102400];

 long totalReadLength = 0;

 int readLength = bufferedInputStream.read(data);

 while (readLength != -1) {

  if (getDownloadManager().isDownloadPaused()) {
   ret = DOWNLOAD_PAUSED;
   break;
  } else if (getDownloadManager().isDownloadCanceled()) {
   ret = DOWNLOAD_CANCELED;
   break;
  } else {

   downloadFile.write(data, 0, readLength);

   totalReadLength = totalReadLength + readLength;

   int downloadProgress = (int)((totalReadLength + existLocalFileLength) * 100 / downloadFileLength);

   getDownloadManager().updateTaskProgress(downloadProgress);

   readLength = bufferedInputStream.read(data);
  }
 }
}

您需要使用MediaScannerConnection扫描保存的文件:

private void scanFile(String path) {

    MediaScannerConnection.scanFile(context,
            new String[] { path }, null,
            new MediaScannerConnection.OnScanCompletedListener() {

                public void onScanCompleted(String path, Uri uri) {
                    Log.d("Tag", "Scan finished. You can view the image in the gallery now.");
                }
            });
}
在existLocalFile的路径上调用此选项:

scanFile(existLocalFile.getAbsolutePath());

就是这样。谢谢你,你能解决我的小问题吗?我的问题链接是