我正在尝试将基于Android的phonegap应用程序的图像上传到Google云端硬盘。我使用以下代码获取图像URI并使用phonegap文件传输插件上传到Google。我正在寻找如何使用phonegap API获取文件名。我使用代码获得的文件名和路径并没有给我真正的文件路径和名称应该是“image.jpg”格式,而是它提供以下格式 - 内容://媒体/外部/图像/媒体/ 47 我搜索过并发现有一些方法可以将URI转换为绝对路径(Get filename and path from URI from mediastore)但所有都是本机代码。我正在使用phonegap框架。
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 :(得分: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;
}
}