下载的图像显示在 android 个工作室中,但未显示在图库中
Downloaded image showing in android studio but not gallery
我正在尝试通过我创建的服务使用 url 下载图像,当我通过 Android Studio 调试器检查我设备的文件目录时,我可以在设备:
红色文件是有问题的下载文件。绿色图片文件是我通过Windows资源管理器手动拖入目录的文件。
我可以在我的图库应用程序中成功看到并打开绿色文件,但找不到红色文件(以及该目录中列出的所有其他文件)。我在 Windows Explorer 中也看不到它们。我是否应该在我的代码中将它们识别为图像,以便文件系统知道它是图像?
这是我下载图片的部分:
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());
我正在尝试通过我创建的服务使用 url 下载图像,当我通过 Android Studio 调试器检查我设备的文件目录时,我可以在设备:
红色文件是有问题的下载文件。绿色图片文件是我通过Windows资源管理器手动拖入目录的文件。
我可以在我的图库应用程序中成功看到并打开绿色文件,但找不到红色文件(以及该目录中列出的所有其他文件)。我在 Windows Explorer 中也看不到它们。我是否应该在我的代码中将它们识别为图像,以便文件系统知道它是图像?
这是我下载图片的部分:
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());