我有个从服务器下载相片的功能
在使用 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的图库组件,本人还是不太懂,所以要各位自己去研究了。

最新文章

  1. Google C++ 风格指南 命名约定 转
  2. SQL Server 2008 R2不支持limit(限制行数)
  3. 推荐cms
  4. 黑马程序员——JAVA基础之JDK1.5新特性高级for循环和可变参数
  5. Miller-Rabin素数测试学习小计
  6. 在Image控件中绘制文字
  7. nodejs中的 Cannot read property&#39;text&#39; of undefined 问题
  8. hdu 3998 (dp+最大流)
  9. vue项目引入bootstrap、jquery
  10. 查看当前支持的shell,echo -e相关转义符,一个简单shell脚本,dos2unix命令把windows格式转为Linux格式
  11. HTTP状态码作用
  12. Vue项目分环境打包的实现步骤
  13. linux常用命令 sort排序命令
  14. Android 按钮响应事件的几种方式
  15. [20180317]12c TABLE ACCESS BY INDEX ROWID BATCHED.txt
  16. 005-spring boot 2.0.4-jdbc升级
  17. JS实现拖动效果
  18. 软件工程项目基于java的wc实现
  19. 集合之HashSet
  20. 配置JDBC

热门文章

  1. apache 日志轮询 linux cronolog
  2. iOS 导航栏实现总结
  3. ORA-07445&amp;ORA-00108错误案例
  4. view组件的duplicateParentState属性
  5. EF深入系列--细节
  6. 海量IT资料 + 各种平台下的Oracle安装文件 + 公开课录像 + 各种视频教程资料
  7. 记一次使用openrowset 的坑
  8. SQL Server调优系列基础篇(并行运算总结篇二)
  9. Storm系列(一):搭建dotNet开发Storm拓扑的环境
  10. CStdioFile CString 读写中文