how to pick image from webview in android - android

I work upon Image Base64 encode/decode application. so i want to pick image from google image in webview and convert into Base64 string.
so how i can pick image on touch event in webview.
Thanks in advance.

You need to get the html content from webiview, and get the image that is in tags

Try... HTTPRequest
Split all image tag.
Get each url.
You will get all url of image

Use a webview with a custom WebViewClient, and override the onLoadResource to capture the image loading.
#Override
public void onLoadResource(WebView view, String url)
{
// debug the page to get the URL format then create filter for images
// capture image URL here
}
After you got the needed URL, convert the URL to a bitmap
sample function:
public Bitmap getBitmapFromURL(String src) {
try
{
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
}
catch (IOException e)
{
//TODO on error
return null;
}
}

Related

Load image from Google Places in Android

I'm having trouble with loading an image from a URL into my Android app using the Google Places API.
I got the part with the API working and I can see the image if I type the URL in my browser. The code fails in the below line:
Bitmap myBitmap = BitmapFactory.decodeStream(input);
The strange thing is, it simply jumps to the return null in the catch clause without executing the println commands.
I should also explain that I'm using two URL's since the first URL in the google API is a redirect page.
I assume that the problem is that the final page that's being loaded isn't actually a "real" image (I mean it looks like
https://lh4.googleusercontent.com/XXX/s1600-w400/
rather than something like
http://something/image.jpg)
Below is the entire method I'm using:
public Bitmap getImageData(Place p) {
try {
String src= "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference="+
p.getPhoto_reference()+
"&key="+
this.context.getResources().getString(places_api);
Log.d("srcsrc", src);
URL url = new URL(src);
HttpsURLConnection ucon = (HttpsURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
HttpsURLConnection connection = (HttpsURLConnection) secondURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Bitmap exception" + e);
return null;
}
}
Appreciate your help.
use Picasso like this :
Picasso.with(context).load(src).into(imageView );
It will do the following:
Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.

How can I load an image from URL without extension android

I want to load an image from a URL but it doesn't work because the link doesn't have an extension
Can this be solved???
URL example :
http://t0.gstatic.com/images?q=tbn:ANd9GcRf1EqE2kyW12HSb9gZZ8eTIPqNgVkjFis4GkTTYONIpoQtkIde4zybZ4iAqGlIHQ_pnEX499Oa
How can this be done??
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
Bitmap bitImg = getBitmapFromURL("http://t0.gstatic.com/images?q=tbn:ANd9GcRf1EqE2kyW12HSb9gZZ8eTIPqNgVkjFis4GkTTYONIpoQtkIde4zybZ4iAqGlIHQ_pnEX499Oa");
img.setImageBitmap(bitImg);
}
public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
look at this post, i dont see his URL but i think Android libraries figure it out based on the data and headers:
BitmapFactory.decodeStream returning null when options are set
also, don't add an extension to a URL, it changes the URL!
http://www.google.com is not the same as http://www.google.com.html
load an image from a URL but it doesn't work because the link doesn't have an extension. Nonsense. Url.openconnection does not look at extensions. It does not work because you have a NetworkOnMainThreadException clearly visible in the LogCat. Never seen in the LogCat? Please go and look for it. Place your network code in an AsyncTask or thread to prevent this.

cookies with getBitmapFromURL()

I am passing the url to get the images from the server e.g.
getBitmapFromURL("http://abc.xyz.in/logo.jpg");
But it is not returning anything. Saying that images are private. So my question is that is there anyway to pass the cookie to getBitmapFromURL() method. So that i can get the images. Or any other alternative is there? Thanks a lot.
2014, if the answer is feasible to you please raise the answer and points
This should do the trick:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Don't forget to add the internet permission and any other permission which is needed in your manifest.
I got my answer. I can do this by using following line:
connection.addRequestProperty("key", value);

Image XML Parsing using sax in android

I want to parse the images in the xml file. i am able to parse the text but how to parse the images and display image corresponding to that text in list view.Image and Text should be in one row. My Xml file sample is below
<tv>
<channel id="0240.TEN ACTION.in">
<display-name>0: TEN ACTION+</display-name>
<thumb_url>http://localhost/rg.jpeg</thumb_url>
</channel>
<channel id="2130.ENTER 10.in">
<display-name>0: ENTER 10</display-name>
<thumb_url>http://localhost/rg.jpeg</thumb_url>
</channel>
</tv>
Try following code:
First you need to get the image path from xml like any other attribute of xml and then pass that URL to this function.
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Nothing too complicated – simply create a URL object using the “src” string (i.e. String src = “http://thinkandroid.wordpress.com”), connect to it, and use Android’s BitmapFactory class to decode the input stream. The result should be your desired Bitmap object!

Getting MalformedURLException: Protocol not found?

I am getting MalformedURLException: Protocol not found, when I am trying to load image from URL sting. That image is displaying in Browser when I paste that URL. But not displaying in ImageView. I've used this code:
public static Bitmap getBitmapFromURL(String src) {
try
{
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
Please anybody tell me whats the problem? It will be very helpful to me.
Its a
http://74.208.68.90/webservice/images/image44.png
Your code is OK.
malformedexception comes when there is some problem in your url
try to encode your parameters in url with url encoder and protocall not found shows
http is missing from your url
String url = url +"/" + UrlEncoder.encode(param);
I think it is "src" that may have problem,

Categories

Resources