PhoneGap奇怪的现象:File FileTransfer download, 手机相册检测不到下载下来的图片(解决)
我有个从服务器下载相片的功能
在使用 File FileTransfer download api时,碰到了很奇怪的现象:
图片已经从服务器里下载了,手机文件夹里也可以看到下载过来的图片,但是我的手机相册就是没有检测到它。
当我把手机上sdcard/Android/data/com.android.gallery3d/cache文件夹删掉,再打开手机相册时,就检测到了。
请问有没有人遇到同样的问题,怎么破,我不可能每次下载完成后都要去删掉那个文件夹吧?????
手机:中兴u759
系统:Android 4.0.4
PhoneGap: 2.8.1
// file: 是我新建的,如: file:///mnt/sdcard/image.jpg
phoneGap.imageManager.download = function(file) {
var fileTransfer = new FileTransfer();
var uri = $("#itemImage").attr('src'); //这里的uri:http://ip:8080/xxx/apple.jpg
var filePath = file.fullPath;
fileTransfer.download(
uri,
filePath,
function(entry) {
cbx.msg.alert("image have been downloaded into: " + entry.fullPath);
},
function(error) {
console.error("download error source: " + error.source);
console.error("download error target: " + error.target);
console.error("upload error code " + error.code);
},
true
);
}
}
以上问题终于在帖子:
找到答案了。 问题出现在phonegap在下载图片成功后,并没有帮我们去刷新手机图库,
所以只有在我们重启手机或者手动删除sdcard/Android/data/com.android.gallery3d/cache文件, 图库才会检测到下载下来的图片。
解决办法:在下载图片成功后,马上刷新图库。
这需要写一个组件,然后用js调用(不明白组件的用途请参照官网:http://docs.phonegap.com/en/2.8.0/guide_plugin-development_index.md.html#Plugin%20Development%20Guide)
大概分四步吧:
#1. 定义组件类
红色标识的: Context context = cordova.getContext();(有错误)
被我改为Context context = cordova.getActivity();
/**
*
*/
package com.greenqloud.plugin; import java.io.File;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri; /**
* @author Philipp Veit (for GreenQloud.com)
* File newImage = new File("/mnt/sdcard/Download/google.png");
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(newImage));
sendBroadcast(scanIntent);
*/
@SuppressWarnings("deprecation")
public class PluginRefreshMedia extends Plugin { /**
* Executes the request and returns PluginResult.
*
* @param action
* The action to execute.
* @param args
* JSONArry of arguments for the plugin.
* @param callbackId
* The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("refresh")) {
String filePath = checkFilePath(args.getString(0));
if (filePath.equals("")) {
return new PluginResult(PluginResult.Status.ERROR);
}
File file = new File(filePath);
Intent scanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(file));
// For more information about cordova.getContext() look here:
// http://simonmacdonald.blogspot.com/2012/07/phonegap-android-plugins-sometimes-we.html?showComment=1342400224273#c8740511105206086350
// Context context = cordova.getContext();
Context context = cordova.getActivity();
context.sendBroadcast(scanIntent);
}
return new PluginResult(status, result);
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
return new PluginResult(PluginResult.Status.ERROR);
}
} private String checkFilePath(String filePath) {
String return_value = "";
try {
return_value = filePath.replaceAll("^file://", "");
} catch (Exception e) {
System.out.println("Error with the filePath");
}
return return_value;
}
}
#2. 定义组件(提供对外接口)
config.xml:
<plugins>
<plugin name="pluginRefreshMedia" value="com.greenqloud.plugin.PluginRefreshMedia"/>
</plugins>
#3. 定义使用组件方法
phoneGap.mediaManager.refresh = function(url, successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, "pluginRefreshMedia", "refresh", [ url ]);
};
#4. 使用组件方法
// file: 是我新建的,如: file:///mnt/sdcard/image.jpg
phoneGap.imageManager.download = function(file) {
var fileTransfer = new FileTransfer();
var uri = $("#itemImage").attr('src'); //这里的uri:http://ip:8080/xxx/apple.jpg
var filePath = file.fullPath;
fileTransfer.download(
uri,
filePath,
function(entry) {
phoneGap.mediaManager.refresh(entry.fullPath,
function success() {
console.info("download complete: ++++++++++" + entry.fullPath);
},
function error() {
console.error("refreh gallery fail: +++++++++++++" + entry.fullPath);
});
},
function(error) {
console.error("download error source: " + error.source);
console.error("download error target: " + error.target);
console.error("upload error code " + error.code);
},
true
);
}
}
以上问题涉及到android的图库组件,本人还是不太懂,所以要各位自己去研究了。
PhoneGap奇怪的现象:File FileTransfer download, 手机相册检测不到下载下来的图片(解决)的更多相关文章
- route 一个很奇怪的现象:我的主机能ping通同一网段的其它主机,并也能xshell 远程其它的主机,而其它的主机不能ping通我的ip,也不能远程我和主机
一个很奇怪的现象:我的主机能ping通同一网段的其它主机,并也能xshell 远程其它的主机,而其它的主机不能ping通我的ip,也不能远程我和主机. [root@NB Desktop]# route ...
- 【Windows 7】发现一个奇怪的现象
最近在Windows7-32位操作系统上发现一个奇怪的现象,不知道64位操作系统上会不会发生这个现象.这个现象就是:如果系统上的一个或多个账户没有设置密码,那么在此条件下终止winlogon.exe进 ...
- 一个关于ExecutorService shutdownNow时很奇怪的现象
我们知道很多类库中的阻塞方法在抛出InterruptedException后会清除线程的中断状态(例如 sleep. 阻塞队列的take),但是今天却发现了一个特别奇怪的现象,先给出代码: publi ...
- Android 手机卫士--xutils说明与下载方法使用
xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词) ...
- 微信公众号与HTML 5混合模式揭秘2——分享手机相册中照片
本书是分享微信jssdk开发的第二篇. 4.2.1 项目需求 需求说明:实现微信端的手机用户,点击按钮选取1张图片,分享到朋友圈. 4.2.2 需求分解 通过对需求的了解,可以将其分解为: ( ...
- 手机相册管理(gallery) ---- HTML5+
模块:gallery Gallery模块管理系统相册,支持从相册中选择图片或视频文件.保存图片或视频文件到相册等功能.通过plus.gallery获取相册管理对象. 管理我们手机上用到的相册:选择图片 ...
- WebApp调用手机相册或摄像头、拨打电话
WebApp调用手机相册或摄像头.拨打电话 一.总结 一句话总结:input标签,指定type为file,选择好对应的accept即可.camera——相机,相应的accept为image : cam ...
- opencv人脸识别提取手机相册内人物充当数据集,身份识别学习(草稿)
未写完 采用C++,opencv+opencv contrib 4.1.0 对手机相册内人物opencv人脸识别,身份识别学习 最近事情多,介绍就先不介绍了 photocut.c #include & ...
- iOS 从手机相册里选取图片
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
随机推荐
- Java基础知识学习(一)
部门接了新项目,后台使用Java框架play framework,前端是html,前后台通过rest交互,能够支持多端的互联网架构. 因为之前没有Java基础,前端使用的也很少,决定深入学习一下Jav ...
- Java中的基本数据类型
什么是基本数据类型 就是我们在编程的时候经常需要用到的数据类型,如整型,浮点型等,把这些数据类型专门拿出来特殊对待,并想象成所谓的“基本类型”. Java中有哪些基本数据类型
- coursera机器学习笔记-神经网络,初识篇
#对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...
- MySQL 调优基础(四) Linux 磁盘IO
1. IO处理过程 磁盘IO经常会成为系统的一个瓶颈,特别是对于运行数据库的系统而言.数据从磁盘读取到内存,在到CPU缓存和寄存器,然后进行处理,最后写回磁盘,中间要经过很多的过程,下图是一个以wri ...
- 烂泥:nginx负载均衡
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 今天我们来学习下有关nginx的负载均衡配置.nginx的负载均衡是通过nginx的upstream模块和proxy_pass反向代理来实现的. 说明: ...
- node命令
- actionlib的身世之谜
不知道为什么会把这么严肃认真的一篇技术整理贴起这么一个故事会风格类似的名字,就这样吧:^)shenmegui 园子里有人整理了actionlib的初学者教程,我来整理下actionlib的细节描述吧. ...
- [译] libvirt 虚机的生命周期 (Libvirt Virtual Machine Lifecycle)
翻译自:http://wiki.libvirt.org/page/VM_lifecycle 这篇文章描述虚机生命周期的基本概念.其目的在于在一篇文章中提供完整的关于虚机创建.运行.停止.迁移和删除 ...
- 查看centos系统版本
1.查看系统版本 [root@yl-web yl]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) 2.查看内核版本 [ro ...
- position:absolute/relative/fixed小结
1.绝对定位:position:absolute; 当一个div块的位置被定义为绝对定位absolute时,也就意味着它失去了文档流的位置,后面的文档流会紧跟着补上来接替它的位置.如果上下左右的绝对偏 ...