2014-02-18 20 views
0

我正嘗試將基於Android的phonegap應用程序的圖像上傳到Google驅動器。我使用以下代碼獲取圖像URI並使用phonegap文件傳輸插件上傳到Google。我正在尋找如何使用phonegap API獲取文件名。我使用代碼獲得的文件名和路徑不會給我真正的文件路徑和名稱,它應該是「image.jpg」格式,而是以下面的格式給出 - content:// media/external/images/media/47 我已經搜索並發現有辦法將URI轉換爲絕對路徑(Get filename and path from URI from mediastore),但都是本地代碼。我正在使用phonegap框架。從phonegap獲取實際的文件名API

uploadFile: function() { 
     navigator.camera.getPicture(
       uploadPhoto, 
       function(message) { console.log("Failed to get pic"+message); }, 
       { 
        quality   : 50, 
        destinationType : navigator.camera.DestinationType.NATIVE_URI, 
        sourceType  : navigator.camera.PictureSourceType.PHOTOLIBRARY 
       } 
       ); 


      function uploadPhoto(imageURI) { 
       console.log("File read"+imageURI); 


       var options = new FileUploadOptions(); 

       //options.fileKey="file.png"; 
       //options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); 
       //options.fileName="googfile.png"; 
       //options.mimeType="image/png"; 

       var params = {}; 
       params.uploadType = "media"; 
       options.params = params; 

       var headers={'Content-Type': 'image/png', 'Authorization': 'Bearer '+localStorage.access_token}; 
       options.headers = headers; 


       var ft = new FileTransfer(); 
       //console.log("File name ="+options.fileName); 
       //console.log("File transfer URI ="+imageURI); 
       ft.upload(imageURI, encodeURI("https://www.googleapis.com/upload/drive/v2/files"), win, fail, options); 
      } 

      function win(resp) { 
       console.log("Code = " + resp.responseCode); 
       console.log("Response = " + resp.response); 
       console.log("Sent = " + resp.bytesSent); 
      } 

      function fail(error) { 
       alert("An error has occurred: Code = " + error.code); 
       console.log("upload error source " + error.source); 
       console.log("upload error target " + error.target); 
      } 
    } 

}; 
+0

是你能解決這個我也在看這個? – greaterKing

+0

@greaterKing。我自己找到了答案,並在下面提到。歡迎任何評論。 – sarfaraz

回答

2

添加JS文件,內容如下 -

window.getFileFromURI = function(successCallback, errorCallback, uri) { 
    cordova.exec(successCallback, errorCallback, "ContentName", "getFileName", [uri]); 
} 

添加Java文件(Android的源)與以下內容 -

package org.apache.cordova.contentname; 

import org.apache.cordova.CallbackContext; 
import org.apache.cordova.CordovaPlugin; 
import org.json.JSONArray; 
import org.json.JSONException; 

import android.content.CursorLoader; 
import android.database.Cursor; 
import android.net.Uri; 
import android.provider.MediaStore; 
import android.util.Log; 

    /** 
    * This class gets the actual file path and name from native side to JavaScript. 
    */ 
    public class ContentName extends CordovaPlugin { 

     @Override 
     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
      if (action.equals("getFileName")) { 
       String uri = args.getString(0); 
       this.getRealPathFromURI(uri, callbackContext); 
       return true; 
      } 
      return false; 
     } 

     private void getRealPathFromURI(String uri, CallbackContext callbackContext) { 
      if (uri != null && uri.length() > 0) { 
       callbackContext.success(this.getFile(uri)); 
      } else { 
       callbackContext.error("Expected one non-empty string argument."); 
      } 
     } 

     private String getFile(String uri) { 

      Uri contentUri = Uri.parse(uri); 
      String[] proj = { MediaStore.Images.Media.DATA }; 
      CursorLoader loader = new CursorLoader(cordova.getActivity(), contentUri, proj, null, null, null); 
      Cursor cursor = loader.loadInBackground(); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      String fileName = cursor.getString(column_index); 
      Log.d("", "********************************************"); 
      Log.d("", fileName); 
      Log.d("", "********************************************"); 
      return fileName; 
     } 


}