我正在尝试通过我创建的服务使用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);
}
}
}
答案 0 :(得分:1)
您需要使用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());