Java 如何使用MediaScannerConnection扫描文件?
我正在将图像下载到SD卡上的文件夹中。由于图像和我的文件夹在多媒体资料中不立即可见,我正在尝试使用MediaScannerConnection更新并显示多媒体资料中的文件夹/图像。 演示如何在视图代码中执行此操作Java 如何使用MediaScannerConnection扫描文件?,java,android,Java,Android,我正在将图像下载到SD卡上的文件夹中。由于图像和我的文件夹在多媒体资料中不立即可见,我正在尝试使用MediaScannerConnection更新并显示多媒体资料中的文件夹/图像。 演示如何在视图代码中执行此操作 private void downloadImage() { if (future != null) { //set the callback and start downloading future.withResponse().setCall
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);
}
}
});
}
}
private void downloadImage(){
如果(未来!=null){
//设置回调并开始下载
future.withResponse().setCallback(新的FutureCallback()){
@凌驾
未完成公共无效(异常e,响应结果){
布尔成功=假;
如果(e==null&&result!=null&&result.getResult()!=null){
试一试{
//准备文件名
字符串url=mSelectedImage.getUrl();
字符串文件名=url.substring(url.lastIndexOf('/')+1,url.length());
//在缓存文件夹中创建临时目录
File dir=Utils.getAlbumStorageDir(“wall tx”);
//创建文件
文件=新文件(目录,文件名);
如果(!file.exists()){
createNewFile();
}
//将图像复制到此文件中
Utils.copyInputStreamToFile(result.getResult(),文件);
//为第一个元素设置动画
animateCompleteFirst(真);
//广播媒体扫描程序以触发它
成功=真实;
}捕获(例外情况除外){
Log.e(“walltx”,例如toString());
}
//完成后制作动画
动画完成(成功);
}否则{
动画设置(真);
}
}
});
}
}
不要只转储代码块。包括注释并解释它的工作原理。代码自我解释,因为我使用了media scanner连接,它随我一起传递,在某些情况下(如操作处理程序)很容易调试它,main活动。此
或。此
比此
更好,因为scanFile
的第一个参数是上下文,而不是回调类。不要只转储代码块。包括注释并解释它的工作原理。代码自我解释,因为我使用了media scanner连接,它随我一起传递,在某些情况下(如操作处理程序)很容易调试它,main活动。此
或。此
比此
更好,因为scanFile
的第一个参数是上下文,而不是回调类。不要只转储代码块。包括注释并解释它的工作原理。代码自我解释,因为我使用了media scanner连接,它随我一起传递,在某些情况下(如操作处理程序)很容易调试它,main活动。此
或。此
比此
更好,因为scanFile
的第一个参数是上下文,而不是回调类。不要只转储代码块。包括注释并解释它的工作原理。代码自我解释,因为我使用了media scanner连接,它随我一起传递,在某些情况下(如操作处理程序)很容易调试它,main活动。此
或。此
比此
更好,因为scanFile
的第一个参数是上下文,而不是回调类。
MediaScannerConnection.scanFile(this, new String[]{file.getPath()},
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// now visible in gallery
}
});