Android: Programmatically Get IMG SRC from Image Gallery within WebView
I'm using a WebView which displays some nice HTML. One of the pictures should be chosen from the Picture Gallery and displayed within the WebView.
So, basically, I need Javascript to access Java (in order to open the Gallery), as well as Java to access Javascript (to get the result back)
I used the following code:
public class JavaScriptInterface
{
Context mContext;
Uri currImageURI;
JavaScriptInterface(Context c)
{
mContext = c;
}
public void imageFromGallery()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
String retResult="";
if (resultCode == RESULT_OK)
{
if (requestCode == 1)
{
currImageURI = data.getDa开发者_开发技巧ta();
retResult=getRealPathFromURI(currImageURI);
}
}
mWebView.loadUrl("javascript:updateImage("+retResult+")");
}
public String getRealPathFromURI(Uri contentUri)
{
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( contentUri,proj,null,null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
Now, calling Java from my script works fine. I added a onClick="Android.imageFromGallery()"
, and it correctly opens the Gallery.
BTW - It gives me the choice between Astro, File manager and Gallery. How can I limit it to Gallery so that it doesn't ask the user which file browser to use?
Anyway, the problem comes with Java calling Javascript to pass the result.
Apparently updateImage()
(my JS function which simply shows the image name) gets no call from the Java code.
What am I doing wrong here?
EDIT: solved the first issue - I can directly call the Gallery using
Intent getImageFromGalleryIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(getImageFromGalleryIntent, 1);
Solved! The mistake was to add the onActivityResult as a function inside the JavascriptInterface class, while it needed to be outside it, within the WebView definition.
(I couldn't answer to my own question before because my reputation isn't high enough, so I had to wait a few hours)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == 1)
{
Uri currImageURI = data.getData();
String retResult = getRealPathFromURI(currImageURI);
mWebView.loadUrl("javascript:updateImage('"+retResult+"')");
}
}
}
public String getRealPathFromURI(Uri contentUri)
{
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( contentUri,proj,null,null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public class JavaScriptInterface
{
Context mContext;
Uri currImageURI;
JavaScriptInterface(Context c)
{
mContext = c;
}
public void imageFromGallery()
{
Intent getImageFromGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(getImageFromGalleryIntent, 1);
}
}
}
精彩评论