我有个从服务器下载相片的功能
在使用 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
);
}
}

以上问题终于在帖子:

http://stackoverflow.com/questions/11954604/cannot-find-file-in-the-gallerie-after-downloading-it-with-phonegap-in-android

找到答案了。 问题出现在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, 手机相册检测不到下载下来的图片(解决)的更多相关文章

  1. route 一个很奇怪的现象:我的主机能ping通同一网段的其它主机,并也能xshell 远程其它的主机,而其它的主机不能ping通我的ip,也不能远程我和主机

    一个很奇怪的现象:我的主机能ping通同一网段的其它主机,并也能xshell 远程其它的主机,而其它的主机不能ping通我的ip,也不能远程我和主机. [root@NB Desktop]# route ...

  2. 【Windows 7】发现一个奇怪的现象

    最近在Windows7-32位操作系统上发现一个奇怪的现象,不知道64位操作系统上会不会发生这个现象.这个现象就是:如果系统上的一个或多个账户没有设置密码,那么在此条件下终止winlogon.exe进 ...

  3. 一个关于ExecutorService shutdownNow时很奇怪的现象

    我们知道很多类库中的阻塞方法在抛出InterruptedException后会清除线程的中断状态(例如 sleep. 阻塞队列的take),但是今天却发现了一个特别奇怪的现象,先给出代码: publi ...

  4. Android 手机卫士--xutils说明与下载方法使用

    xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词) ...

  5. 微信公众号与HTML 5混合模式揭秘2——分享手机相册中照片

    本书是分享微信jssdk开发的第二篇.     4.2.1 项目需求 需求说明:实现微信端的手机用户,点击按钮选取1张图片,分享到朋友圈. 4.2.2 需求分解 通过对需求的了解,可以将其分解为: ( ...

  6. 手机相册管理(gallery) ---- HTML5+

    模块:gallery Gallery模块管理系统相册,支持从相册中选择图片或视频文件.保存图片或视频文件到相册等功能.通过plus.gallery获取相册管理对象. 管理我们手机上用到的相册:选择图片 ...

  7. WebApp调用手机相册或摄像头、拨打电话

    WebApp调用手机相册或摄像头.拨打电话 一.总结 一句话总结:input标签,指定type为file,选择好对应的accept即可.camera——相机,相应的accept为image : cam ...

  8. opencv人脸识别提取手机相册内人物充当数据集,身份识别学习(草稿)

    未写完 采用C++,opencv+opencv contrib 4.1.0 对手机相册内人物opencv人脸识别,身份识别学习 最近事情多,介绍就先不介绍了 photocut.c #include & ...

  9. iOS 从手机相册里选取图片

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

随机推荐

  1. 按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有确定位

    package com.hanqi.test; public class Rect { ; ; public double getWidth() { return width; } public vo ...

  2. 使用ImitateLogin模拟登录百度

    在之前的文章中,我已经介绍过一个社交网站模拟登录的类库:imitate-login ,这是一个通过c#的HttpWebRequest来模拟网站登录的库,之前实现了微博网页版和微博Wap版:现在,模拟百 ...

  3. python爬取并下载麦子学院所有视频教程

    一.主要思路 scrapy爬取是有课程地址及名称 使用multiprocessing进行下载 就是为了爬点视频,所以是简单的代码堆砌 想而未实行,进行共享的方式 二.文件说明 itemsscray字段 ...

  4. nodeJS Express 删除 x-powered-by

    在使用Express4 Header头部会输出,在晚上搜索几种方案也没有产生效果,就看了一下官方文档 Property Type               Value Default     x-p ...

  5. Page Visibility(页面可见性) API介绍、微拓展[转]

    一.网页君的悲情谁来懂 唉,突然想到了一首悲情诗: 泪湿罗巾梦不成,夜深前殿按歌声.红颜未老恩先断, 斜倚薰笼坐到明. 学生时代学过的一首诗,已还给老师不知所云的诸位可参见下面释义: 诗的主人公是一位 ...

  6. SSH----MVC框架模式与分层架构

    MVC框架模式 MVC框架模式是web开发中一种软件设计典范,他的全名是(Model -View -Controller),是模型(model)--视图(view)--控制器(controller)的 ...

  7. POJ1270 Following Orders[拓扑排序所有方案 Kahn]

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4885   Accepted: 1973 ...

  8. 第六章、Struts2数据校验

    一.三种实现方式 ① 用validate()方法实现数据校验 继承ActionSupport类,该类实现了Validateable接口,该接口中定义了一个validate()方法,在自定义的Actio ...

  9. chrome插件编写之新版hello world

    编写chrome插件之前,需要熟悉一下相应的chrome插件开发环境.从编写hello world开始,参考阅读官方的教程,是一个不错的选择.这里主要是基于chrome的官方教程,稍稍做了一些修改和扩 ...

  10. jQuery语法

    目录: 一.选择网页元素二.改变结果集三.链式操作四.元素的操作五.元素的操作:移动六.元素的操作:复制.删除和创建七.工具方法八.事件操作九.特殊效果 一.选择网页元素这也是jQuery的基本设计思 ...