Android 如何从Javascript启动ActivityResult

Android 如何从Javascript启动ActivityResult,android,Android,我的android webView应用程序面临一个大问题。当时一切正常,但当我需要从手机厨房上传图片时遇到了问题。我可以从Javascript显示手机图像库,但无法启动onActivityResult,这就是为什么无法获取所选图像的图像路径。我的代码如下: public class Hello extends Activity { WebView webview; @Override public void onCreate(Bundle savedInstanceState) {

我的android webView应用程序面临一个大问题。当时一切正常,但当我需要从手机厨房上传图片时遇到了问题。我可以从Javascript显示手机图像库,但无法启动onActivityResult,这就是为什么无法获取所选图像的图像路径。我的代码如下:

public class Hello extends Activity {   
WebView webview;    
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       

    setContentView(R.layout.main);

    webview = (WebView) findViewById(R.id.webview);

    webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");

    webview.setWebViewClient(new HelloWebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);

    webview.loadUrl("http://someurl.com/Login.aspx");

}                    
private static final int SELECT_PICTURE = 1;
private static final int PICK_IMAGE = 1;

private String selectedImagePath;
//ADDED
private String filemanagerstring;

public class JavaScriptInterface {
       Context mContext;

       /** Instantiate the interface and set the context */
       JavaScriptInterface(Context c) {
          mContext = c;          
       }
       /** Show a toast from the web page */
        public String showToast(String toast) {

                 try {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"),PICK_IMAGE);      

                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
                    Log.e(e.getClass().getName(), e.getMessage(), e);
                }                                               
             return selectedImagePath;
        }


         protected void onActivityResult(int requestCode, int resultCode, Intent data){
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_PICTURE) {
                    Uri selectedImageUri = data.getData();

                    //OI FILE Manager
                    filemanagerstring = selectedImageUri.getPath();

                    //MEDIA GALLERY
                    selectedImagePath = getPath(selectedImageUri);

                    //DEBUG PURPOSE - you can delete this if you want
                    if(selectedImagePath!=null)
                        System.out.println(selectedImagePath);
                    else System.out.println("selectedImagePath is null");
                    if(filemanagerstring!=null)
                        System.out.println(filemanagerstring);
                    else System.out.println("filemanagerstring is null");

                    //NOW WE HAVE OUR WANTED STRING
                    if(selectedImagePath!=null)
                        System.out.println("selectedImagePath is the right one for you!");
                    else
                        System.out.println("filemanagerstring is the right one for you!");

                }
            }
        }

        public String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            if (cursor != null) {
                // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
                // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }

    }
}

从Javascript中:

<script type="text/javascript" language="javascript">

   var Imagepath= Android.showToast('Hello This is Test From JS');  

</script>

var Imagepath=Android.showtoos('您好,这是来自JS的测试');

谁能帮我找出问题所在吗。我是android的新手

您在Javascript接口类中定义的是
onActivityResult()
,而不是在活动中。把它移到你的活动课上