gpt4 book ai didi

java - 如何使用MediaScannerConnection扫描文件?

转载 作者:行者123 更新时间:2023-12-01 22:17:04 27 4
gpt4 key购买 nike

我正在将图像下载到 SDCARD 上的文件夹中。由于图像和我的文件夹在图库中不会立即可见,我试图让 MediaScannerConnection 更新并显示图库中的文件夹/图像。向您展示如何在 View 代码中执行此操作?

private void downloadImage() {
if (future != null) {
//set the callback and start downloading
future.withResponse().setCallback(new FutureCallback<Response<InputStream>>() {
@Override
public void onCompleted(Exception e, Response<InputStream> result) {
boolean success = false;
if (e == null && result != null && result.getResult() != null) {
try {
//prepare the file name
String url = mSelectedImage.getUrl();
String fileName = url.substring(url.lastIndexOf('/') + 1, url.length());
//create a temporary directory within the cache folder
File dir = Utils.getAlbumStorageDir("wall-tx");
//create the file
File file = new File(dir, fileName);
if (!file.exists()) {
file.createNewFile();
}

//copy the image onto this file
Utils.copyInputStreamToFile(result.getResult(), file);

//animate the first elements
animateCompleteFirst(true);

//Broadcast the Media Scanner Intent to trigger it

success = true;
} catch (Exception ex) {
Log.e("walltx", ex.toString());
}

//animate after complete
animateComplete(success);
} else {
animateReset(true);
}
}
});
}
}

最佳答案

    MediaScannerConnection.scanFile(this, new String[]{file.getPath()},
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// now visible in gallery
}
});

关于java - 如何使用MediaScannerConnection扫描文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30764647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com