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.
Related
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.
I'm trying to get a static image from a map and put on my Android layout. So, looking for it I've found a code talking about Google Static Maps. Awesome! I've tried some URLs on my PC and it's working perfectly!
But my problem was when I've put the URL in Android to test it. WHen I try to get the input stream (the image), it gives me FileNotFoundExceptioin, because when I tried to connect, it threw me a Bad Request (error 400)
Here is my code:
public class ImageLoadingTask extends AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... stringURL) {
Bitmap bmp = null;
try {
URL url = new URL(stringURL[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
bmp = BitmapFactory.decodeStream(is, null, options);
} catch (IOException e) {
e.printStackTrace();
}
return bmp;
}
#Override
protected void onPostExecute(Bitmap result) {
mapView.setImageBitmap(result);
super.onPostExecute(result);
}
}
String for test
Do I need a key for this?? Why don't I need a key for doing it in a browser?
The URL you're sending to Google has a strange character in it. Before sending it, run a URLEncode.encode() on it or if you're always showing São Paulo you could say "Sao Paulo" and let Google figure it out, or "S%C3%A3o%20Paulo". Personally I think the first one is less error prone but it's up to you.
I'm looking for a very basic function. I'm trying to design an app and all I want it to do is load an image from a URL.
I've found a few questions and websites, but they all seem to be older and dated, but what I think I'm having issues with is linking the code to activity main.xml for ImageView.
Any suggestions or links you have I would greatly appreciate, thank you.
Here, this is how I display image from url in the image view
you have to call this code from thread other than main thread
ImageView img = (ImageView) findViewById(R.id.imageView1);
try {
URL url = new URL("Your URL");
//try this url = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);
img.setImageBitmap(bitmap);
} catch (Exception ex) {
}
Be careful don't forget to surround the code with try catch(I have already done that in this code)
or you can use webview to load image from url
WebView web = (WebView) findViewById(R.id.webView1);
web.loadUrl("Your Url");
if you are trying to load image from the assets folder url will start like this
"file:///android_asset/yourimage.jpg"
else normal internet url like this
"http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"
hope this works for you
Good Luck
There is an opensource library called imageloader. It is widely used, you can use it directly or make code similar to it.
https://github.com/nostra13/Android-Universal-Image-Loader
You can take the image and on your php side convert it into a base64 and then on Android side decode it into a image.
The best practice to download image is to be done on the background Thread, so that it doesn't interrupt your Main Thread,then update the User Interface as needed.
public class MainActivity extends AppCompatActivity {
FrameLayout frameLayout;
ImageView imageView;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout= (FrameLayout) findViewById(R.id.containerlayout);
imageView= (ImageView) findViewById(R.id.imageView);
progressBar= (ProgressBar) findViewById(R.id.progressBar);
String url="http://www.flat-e.com/flate5/wp-content/uploads/cover-960x857.jpg";
MyTask myTask= new MyTask();
myTask.execute(url);
}
class MyTask extends AsyncTask<String, Void, Bitmap> {
#Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
#Override
protected Bitmap doInBackground(String... voids) {
Bitmap bitmap=null;
try {
URL url =new URL(voids[0]);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
InputStream inputStream= connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
progressBar.setVisibility(View.GONE);
imageView.setImageBitmap(bitmap);
}
}
}
Here, in this example I created an inner class MyTask which extends the AsyncTask where i have done all my network operations. Make sure to add the Uses permission in your Manifest File.
Hope this works for you too.
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;
}
}
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,