我有个从服务器下载相片的功能
在使用 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中静态类型检查是如何进行的

    以下内容来自维基百科,关于静态类型检查和动态类型检查的解释: 静态类型检查:基于程序的源代码来验证类型安全的过程: 动态类型检查:在程序运行期间验证类型安全的过程: Java使用静态类型检查在编译期间 ...

  2. OEM代工厂产品经理个人经历谈

    创业不是一件随随便便的事情! 到2007年时,我已经在上海.广州.东莞三地的工厂打工有十来年了.正是这个时间结点,我也即将做父亲了.打了很久的工后,就开始感到疲倦,做来做去,都是给老板做,也就在这时开 ...

  3. 每日Scrum(4)

    今天是冲刺第4天,小组也没有做什么,大家都忙着找大二的学弟学妹来点评来支持我们的软件. 遇到的问题主要是如何劝说学弟学妹选择我们的软件然后继续往下做.

  4. MySQL锁机制总结(二)

    前言: Mysql是一个支持插件式存储引擎的数据库系统,本文讨论的锁机制也主要包含两部分SERVER层的锁和存储引擎的锁,存储引擎是指innodb,其它存储引暂不讨论. 1. 数据库中锁相关的基本概念 ...

  5. 挖一挖C#中那些我们不常用的东西之系列(1)——ToDictionary,ToLookup

    这个系列我们看看C#中有哪些我们知道,但是又不知道怎么用,又或者懒得去了解的东西,比如这篇我们要介绍的toDictionary 和ToLookup. 从图中我们看到有四个ToXXX的方法,其中ToAr ...

  6. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  7. php基础系列:PHP连接MySQL数据库用到的三种API

    参考自php手册.本文没有太大意义,仅为方便自己上网查阅. 1.PHP的MySQL扩展2.PHP的mysqli扩展3.PHP数据对象(PDO) MySQL扩展函数 这是设计开发允许PHP应用与MySQ ...

  8. 【转】XPath 示例

    XPath 示例   其他版本   本主题回顾整个 XPath 参考中出现的语法示例. 所有示例均基于 XPath 语法的示例 XML 文件 (inventory.xml). 有关在测试文件中使用 X ...

  9. margin-top无效的解决方法

    先上代码: <div id="content" style=" width:750px; height:300px; background:#C29A29; mar ...

  10. AI(Adobe Illustrator)简单入门——小熊

    成果: AI里ctrl+z撤销,恢复是ctrl+shift+z. 主要是使用Blob笔刷和橡皮擦工具来做. 一.选择Blog笔刷,选择小熊的颜色. 二.画小熊的头和身子和前脚掌 按住左中括号和右中括号 ...