如何从Javascript触发onActivityResult

时间:2011-05-02 14:57:36

标签: android

我的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>

任何人都可以帮我找出问题。我是android的新手

1 个答案:

答案 0 :(得分:0)

您在Javascript界面​​类中定义onActivityResult(),而不在您的Activity中。将其移至您的Activity类。