这是我的代码
package com.sp.spandroid;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.widget.Toast;
import org.apache.cordova.*;
import java.io.File;
public class CordovaApp extends CordovaActivity
{
private JSBridge jsBridge;
// public static Activity activity;
public static ValueCallback<Uri> mUploadMessage;
private Uri mCapturedImageURI = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
// activity = this;
// Set by <content src="index.html" /> in config.xml
LOG.d("SPANDROID/CordovaApp/onCreate==>","onCreate !!");
jsBridge = new JSBridge(this, super.appView);
super.appView.addJavascriptInterface(jsBridge, "jsBridge");
super.appView.getSettings().setJavaScriptEnabled(true);
super.appView.getSettings().setLoadWithOverviewMode(true);
super.appView.getSettings().setAllowFileAccess(true);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
super.appView.getSettings().setMediaPlaybackRequiresUserGesture(false);
}
loadUrl(launchUrl);
CordovaInterface cdv = (CordovaInterface)getContext();
super.appView.setWebChromeClient(new CordovaChromeClient(cdv,super.appView) {
// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
CordovaApp.mUploadMessage = uploadMsg;
LOG.d("SPANDROID/CordovaApp/openFileChooser==>","acceptType ==>"+ acceptType);
try {
File imageStorageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
, "sp");
if (!imageStorageDir.exists()) {
// Create AndroidExampleFolder at sdcard
imageStorageDir.mkdirs();
}
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
if(acceptType.equals("video/*")){
LOG.d("SPANDROID/CordovaApp/openFileChooser==>","inside video ==>"+ acceptType);
i.setType("video/*");
File file = new File(
imageStorageDir + File.separator + "VIDEO_"
+ String.valueOf(System.currentTimeMillis())
+ ".3gp");
final Intent captureIntent = new Intent(
MediaStore.ACTION_VIDEO_CAPTURE);
mCapturedImageURI = Uri.fromFile(file);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
Intent chooserIntent = Intent.createChooser(i, "File Chooser");
// Set camera intent to file chooser
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
, new Parcelable[] { captureIntent });
LOG.d("SPANDROID/CordovaApp/openFileChooser==>","openFileChooser 11 !!");
this.cordova.getActivity().startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}else {
// Create camera captured image file path and name
File file = new File(
imageStorageDir + File.separator + "IMG_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg");
mCapturedImageURI = Uri.fromFile(file);
// Camera capture image intent
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i, "File Chooser");
// Set camera intent to file chooser
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
, new Parcelable[] { captureIntent });
LOG.d("SPANDROID/CordovaApp/openFileChooser==>","openFileChooser 11 !!");
this.cordova.getActivity().startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
// Create file chooser intent
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Camera Exception:" + e, Toast.LENGTH_LONG).show();
}
}
// For Android < 3.0
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
// For Android > 4.1.1
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg, acceptType);
}
public boolean onConsoleMessage(ConsoleMessage cm) {
onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
return true;
}
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
Log.d("SPANDROID", "SPANDROID: " + message);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
LOG.d("SPANDROID/CordovaApp/onActivityResult==>","resultCode ==>"+resultCode);
LOG.d("SPANDROID/CordovaApp/onActivityResult==>","mUploadMessage ==>"+ CordovaApp.mUploadMessage);
// LOG.d("SPANDROID/CordovaApp/onActivityResult==>","intent.getData() ==>"+intent.getData());
// TODO Auto-generated method stub
if(requestCode==CordovaChromeClient.FILECHOOSER_RESULTCODE)
{
if (null == CordovaApp.mUploadMessage) {
return;
}
Uri result=null;
try{
if (resultCode != RESULT_OK) {
CordovaApp.mUploadMessage.onReceiveValue(null);
return;
} else {
// retrieve from the private variable if the intent is null
result = intent == null ? mCapturedImageURI : intent.getData();
}
}
catch(Exception e)
{
// Toast.makeText(getApplicationContext(), "activity :"+e, Toast.LENGTH_LONG).show();
}
LOG.d("SPANDROID/CordovaApp/onActivityResult==>","result ==>"+result);
CordovaApp.mUploadMessage.onReceiveValue(result);
CordovaApp.mUploadMessage = null;
}
}
}
onActivityResult它将CordovaApp.mUploadMessage作为null获取。当我从相机中选择图像时,应用程序正在重新启动,我无法上传图像。仅在某些手机上才会发生这种情况。像moto-e moto-g这样的手机正在显示上传问题