PhoneGap奇怪的現象:File FileTransfer download, 手機相冊檢測不到下載下來的圖片(解決)

我有個從服務器下載相片的功能
在使用 File FileTransfer download api時,碰到了很奇怪的現象:
圖片已經從服務器裏下載了,手機文件夾裏也能夠看到下載過來的圖片,可是個人手機相冊就是沒有檢測到它。
當我把手機上sdcard/Android/data/com.android.gallery3d/cache文件夾刪掉,再打開手機相冊時,就檢測到了。
請問有沒有人遇到一樣的問題,怎麼破,我不可能每次下載完成後都要去刪掉那個文件夾吧?????html

手機:中興u759
系統:Android 4.0.4java

PhoneGap: 2.8.1android

 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     }

以上問題終於在帖子:apache

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

找到答案了。 問題出如今phonegap在下載圖片成功後,並無幫咱們去刷新手機圖庫,json

因此只有在咱們重啓手機或者手動刪除sdcard/Android/data/com.android.gallery3d/cache文件, 圖庫纔會檢測到下載下來的圖片。api

解決辦法:在下載圖片成功後,立刻刷新圖庫。服務器

這須要寫一個組件,而後用js調用(不明白組件的用途請參照官網:http://docs.phonegap.com/en/2.8.0/guide_plugin-development_index.md.html#Plugin%20Development%20Guideapp

大概分四步吧:ide

#1. 定義組件類ui

紅色標識的: 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的圖庫組件,本人仍是不太懂,因此要各位本身去研究了。

相關文章
相關標籤/搜索