开发者

Android to Javascript Calling issue

I have a android method which one is called from Javascript for getting the selected filepath info from phone gallery. Now i want to send the filepath name to the javascript method but if i return the filepath name from the android function then it will always return the last filepath info not the current one (May be its for callback!) or if i call any javascript method to send the file info after getting the file path then its show on debug console

like that: 05-04 22:07:50.274: ERROR/Web Console(331): ReferenceError: Can't find variable: SetImageFileName at undefined:1

And here is my Code what i have done...

  public class Hello extends Activity {
/** Called when the activity is first created. */   
WebView webview;    
private ProgressDialog progressBar;         
private static final int SELECT_PICTURE = 1;
private static final int PICK_IMAGE = 1;
private String selectedImagePath;  
private String filemanagerstring;
private long siteId;

@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.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webview.setWebViewClient(new HelloWebViewClient());
    WebSettings webSettings = webview.getSettings();

    webSettings.setSavePassword(false);
    webSettings.setSaveFormData(false);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(false);
    webview.loadUrl(getString(R.string.ApplicationWebURL));               
}


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;
}


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

            String filePath = null;

            try {
                // OI FILE Manager
                 filemanagerstring = selectedImageUri.getPath(); 
                // MEDIA GALLERY

                 selectedImagePath = getPath(selectedImageUri);
                 selectedFileName=getFileName(selectedImagePath);

                if (selectedImagePath != null) {
                    filePath = selectedImagePath;
                } 
                else if (filemanagerstring != null) {
                    filePath = filemanagerstring;
                } 
                else {
                    Toast.makeText(getApplicationContext(), "Unknown path",
                    Toast.LENGTH_LONG).show();
                    Log.e("Bitmap", "Unknown path");
                }

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Internal error",
                Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
            }

        }
    }
}

final class JavaScriptInterface {
       Context mContext;

       JavaScriptInterface(Context c) {
          mContext = c;

       }
       /** Show a toast from the web page */
        public String ShowPhoneGallery(long sId) {

                 try {      

                    siteId=sId;

                    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);
                }   
            //Calling Javascript to return the value    
             webview.loadUrl("javascrip开发者_开发技巧t:SetImageFileName('"+ selectedImagePath +"')");


             return selectedImagePath;
        }

    }


}

I s there anybody who can help me on this issue ...


Looks to me like you need to put this line

webview.loadUrl("javascript:SetImageFileName('"+ selectedImagePath +"')");

in a different method which is then called from onActivityResult once your user has selected the picture

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜