我将此插件添加到我的cordova项目中,这使我能够选择文件,然后返回此文件的路径。我的问题是返回的路径不是真正的路径,它是content://media/external/images/media/2144
。我想将此URI转换为实际路径,例如storage/sdcard0/DCIM/camera/321321321.jpg
这是java中的插件代码,我应该修改什么?
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONException;
public class FileChooser extends CordovaPlugin {
private static final String TAG = "FileChooser";
private static final String ACTION_OPEN = "open";
private static final int PICK_FILE_REQUEST = 1;
CallbackContext callback;
@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
if (action.equals(ACTION_OPEN)) {
chooseFile(callbackContext);
return true;
}
return false;
}
public void chooseFile(CallbackContext callbackContext) {
// type and title should be configurable
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
Intent chooser = Intent.createChooser(intent, "Select File");
cordova.startActivityForResult(this, chooser, PICK_FILE_REQUEST);
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
pluginResult.setKeepCallback(true);
callback = callbackContext;
callbackContext.sendPluginResult(pluginResult);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FILE_REQUEST && callback != null) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
if (uri != null) {
Log.w(TAG, uri.toString());
callback.success(uri.toString());
} else {
callback.error("File uri was null");
}
} else if (resultCode == Activity.RESULT_CANCELED) {
// TODO NO_RESULT or error callback?
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
callback.sendPluginResult(pluginResult);
} else {
callback.error(resultCode);
}
}
}
}