我有个从服务器下载相片的功能
在使用 File FileTransfer download api时,碰到了很奇怪的现象:
图片已经从服务器里下载了,手机文件夹里也可以看到下载过来的图片,但是我的手机相册就是没有检测到它。
当我把手机上sdcard/Android/data/com.android.gallery3d/cache文件夹删掉,再打开手机相册时,就检测到了。
请问有没有人遇到同样的问题,怎么破,我不可能每次下载完成后都要去删掉那个文件夹吧?????
手机:中兴u759
系统:Android 4.0.4
PhoneGap: 2.8.1
1 // file: 是我新建的,如: file:///mnt/sdcard/image.jpg 2 phoneGap.imageManager.download = function(file) { 3 var fileTransfer = new FileTransfer(); 4 var uri = $("#itemImage").attr('src'); //这里的uri:http://ip:8080/xxx/apple.jpg 5 var filePath = file.fullPath; 6 fileTransfer.download( 7 uri, 8 filePath, 9 function(entry) { 10 cbx.msg.alert("image have been downloaded into: " + entry.fullPath); 11 }, 12 function(error) { 13 console.error("download error source: " + error.source); 14 console.error("download error target: " + error.target); 15 console.error("upload error code " + error.code); 16 }, 17 true 18 ); 19 } 20 }
以上问题终于在帖子:
找到答案了。 问题出现在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();
1 /** 2 * 3 */ 4 package com.greenqloud.plugin; 5 6 import java.io.File; 7 import org.apache.cordova.api.Plugin; 8 import org.apache.cordova.api.PluginResult; 9 import org.json.JSONArray; 10 import org.json.JSONException; 11 import android.content.Context; 12 import android.content.Intent; 13 import android.net.Uri; 14 15 /** 16 * @author Philipp Veit (for GreenQloud.com) 17 * File newImage = new File("/mnt/sdcard/Download/google.png"); 18 Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 19 scanIntent.setData(Uri.fromFile(newImage)); 20 sendBroadcast(scanIntent); 21 */ 22 @SuppressWarnings("deprecation") 23 public class PluginRefreshMedia extends Plugin { 24 25 /** 26 * Executes the request and returns PluginResult. 27 * 28 * @param action 29 * The action to execute. 30 * @param args 31 * JSONArry of arguments for the plugin. 32 * @param callbackId 33 * The callback id used when calling back into JavaScript. 34 * @return A PluginResult object with a status and message. 35 */ 36 @Override 37 public PluginResult execute(String action, JSONArray args, String callbackId) { 38 PluginResult.Status status = PluginResult.Status.OK; 39 String result = ""; 40 try { 41 if (action.equals("refresh")) { 42 String filePath = checkFilePath(args.getString(0)); 43 if (filePath.equals("")) { 44 return new PluginResult(PluginResult.Status.ERROR); 45 } 46 File file = new File(filePath); 47 Intent scanIntent = new Intent( 48 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 49 scanIntent.setData(Uri.fromFile(file)); 50 // For more information about cordova.getContext() look here: 51 // http://simonmacdonald.blogspot.com/2012/07/phonegap-android-plugins-sometimes-we.html?showComment=1342400224273#c8740511105206086350 52 // Context context = cordova.getContext(); 53 Context context = cordova.getActivity(); 54 context.sendBroadcast(scanIntent); 55 } 56 return new PluginResult(status, result); 57 } catch (JSONException e) { 58 return new PluginResult(PluginResult.Status.JSON_EXCEPTION); 59 } catch (Exception e) { 60 System.out.println("Error: " + e.getMessage()); 61 return new PluginResult(PluginResult.Status.ERROR); 62 } 63 } 64 65 private String checkFilePath(String filePath) { 66 String return_value = ""; 67 try { 68 return_value = filePath.replaceAll("^file://", ""); 69 } catch (Exception e) { 70 System.out.println("Error with the filePath"); 71 } 72 return return_value; 73 } 74 }
#2. 定义组件(提供对外接口)
config.xml:
1 <plugins> 2 <plugin name="pluginRefreshMedia" value="com.greenqloud.plugin.PluginRefreshMedia"/> 3 </plugins>
#3. 定义使用组件方法
1 phoneGap.mediaManager.refresh = function(url, successCallback, errorCallback) { 2 cordova.exec(successCallback, errorCallback, "pluginRefreshMedia", "refresh", [ url ]); 3 };
#4. 使用组件方法
1 // file: 是我新建的,如: file:///mnt/sdcard/image.jpg 2 phoneGap.imageManager.download = function(file) { 3 var fileTransfer = new FileTransfer(); 4 var uri = $("#itemImage").attr('src'); //这里的uri:http://ip:8080/xxx/apple.jpg 5 var filePath = file.fullPath; 6 fileTransfer.download( 7 uri, 8 filePath, 9 function(entry) { 10 phoneGap.mediaManager.refresh(entry.fullPath, 11 function success() { 12 console.info("download complete: ++++++++++" + entry.fullPath); 13 }, 14 function error() { 15 console.error("refreh gallery fail: +++++++++++++" + entry.fullPath); 16 }); 17 }, 18 function(error) { 19 console.error("download error source: " + error.source); 20 console.error("download error target: " + error.target); 21 console.error("upload error code " + error.code); 22 }, 23 true 24 ); 25 } 26 }
以上问题涉及到android的图库组件,本人还是不太懂,所以要各位自己去研究了。