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 ...
随机推荐
- 腾讯Tinker初入门总结
- eclipse js提醒报错
在用eclipse开发项目时,有时候导入项目后,报错为 Problem Occurred: Errors occurred during the build. Errors running bu ...
- Javascript 优化项目代码技巧之语言基础(一)
Javascript的弱类型以及函数作用域等规则使用编写Javascript代码极为容易,但是编写可维护.高质量的代码却变得十分困难,这个系列的文章将总结在项目开发过程中,能够改善代码可读性. ...
- js 判断pc端或手机端
<script> (function () { var navUA = navigator.userAgent; var defIncludeStr = "iPhone|Andr ...
- 使用For XML PATH 会影响Cross Apply 返回
昨天在写语句的时候,遇到了一个现象,其实就是使用 Cross Apply做一个拼接字符串的而已.比如 CREATE TABLE GoodsCatalog (ID INT, Name )) CREATE ...
- 记录Sqlserver2012附加Sqlserver2008的数据库出错的解决方案
一.摘要 最近在实验里面用台式编写好了一个软件,想移植到家里的笔记本上.在附加数据的时候却出现了错误,具体也没有提示什么错误,反正就是附加失败了. 二.解决方案 在网上看了一些资料,有的说[低版本不能 ...
- c#操作IIS站点
/// <summary> /// 获取本地IIS版本 /// </summary> /// <returns></returns> public st ...
- 使用jlink直接烧norflash或者nandflash不借助uboot的猜想
由于喜欢折腾,我是在linux下使用jlink的,既然JLinkExe可以进行内存读写操作,loadbin等操作,并且通过指定命令文件支持批量指令输入,那么首先jlink是可以直接访问内部存储器的,包 ...
- MordenPHP阅读笔记(一)——先跑再说,跑累了再走
---恢复内容开始--- 后台一大堆半成品,或者是几乎不成的... 这本书不错,起码是别人推荐的,然后也是比较新的东西,学哪本不是学嘛,关键是得看. 今儿个网不好,科研所需的代码下不到,看书做笔记吧. ...
- 【2016-10-10】【坚持学习】【Day1】【观察者模式】
今天学习了观察者模式 定义: 一个实体变化会影响其他实体变化 例子: 红绿灯与汽车 红绿灯是观察目标 汽车是实际观察者 灯的变化会影响车是停止还是前进. 例子: 游戏中,一个战队由若干队员组成,当其中 ...