User Agent on a url.getContent(); - android

I need to download one file from an url that i give from an xml after connect to other url. My problem is that i need the same user agent and ip to do both actions.
To obtain the xml, i use a PHP code that is hosted on my server and i send it the user agent of my app:
String ua=new WebView(ct).getSettings().getUserAgentString().trim();
ua = ua.replaceAll(" ", "%20");
Then i parse the xml with a generic RssParserSax that gives me all i need.
After, i try to download the file to a drawable var and i do this:
Drawable dd = ImageOperations(url);
private Drawable ImageOperations(String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
private Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
But as i don't send the user agent, it does not give me anything.

Finally i did this to resolve:
Bitmap bmImg;
try {
URL url = new URL(addres);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent", ua);
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imagen.setImageBitmap(bmImg);
imagen.setScaleType(ScaleType.FIT_XY);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

You can use AndroidHttpClient.newInstance(useragent).
See an example here AndroidHttpClient can not getEntity().getContent() after closed

Related

Display web image in android

I try to load a jpg image in a image view but I can't get the image, I debug the code and I guess to have some issue with permissions but when I try from another public web I have the same problem.
I use this 2 examples published here but I can find where is my error, very thanks for the help!
//option 1
private static Drawable fotoWeb(String address) {
try {
InputStream is = (InputStream) new URL(address).getContent();
//this line don't work crash on getContent()
Drawable d = Drawable.createFromStream(is, "Foto");
return d;
} catch (Exception e) {
return null;
}
}
//option 2
private static Bitmap fotoBMP (String address){
try {
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(60000 /* milliseconds */);
conn.setConnectTimeout(65000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect(); //this line don't work crash on connect()
InputStream is = conn.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
Bitmap bmpImage = BitmapFactory.decodeStream(bufferedInputStream);
return bmpImage;
} catch (Exception e) {
return null;
}
}
The code seems to be fine, just ensure to add the internet permission to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
Much more simplified and enough:
public static Bitmap fotoBMP(String address) {
try {
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
return BitmapFactory.decodeStream(connection.getInputStream());
} catch (IOException ioEx) {
return null;
}
}
I better use the Picasso Library with the result I want very thanks GoRos for the help here is the link I use to see how to use http://javatechig.com/android/how-to-use-picasso-library-in-android

Nullpointer Exception While using Google 3D Pie Chart Tools?

I am using this exact same code from here
Link
to show 3D pie chart in my project .. The code is alright. I am getting the LOG of URL properly and when i use the link in browser its showing the chart properly .. But when i am trying to show the image to an image-view inside my application by converting it to bitmap , Its giving null pointer Exception ,,,
private Bitmap loadChart(String urlRqs){
Bitmap bm = null;
InputStream inputStream = null;
try {
**inputStream = OpenHttpConnection(urlRqs);
bm = BitmapFactory.decodeStream(inputStream);
inputStream.close();**
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bm;
}
private InputStream OpenHttpConnection(String strURL) throws IOException{
InputStream is = null;
URL url = new URL(strURL);
URLConnection urlConnection = url.openConnection();
try{
HttpURLConnection httpConn = (HttpURLConnection)urlConnection;
httpConn.setRequestMethod("GET";
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = httpConn.getInputStream();
}
}catch (Exception ex){
}
return is;
}

How to download a image from URL in App

I'd like to know how I can download an Image from a given URL and display it inside an ImageView. And is there any permissions required to mention in the manifest.xml file?
You need to put this permission to access the Internet
<uses-permission android:name="android.permission.INTERNET" />
you can try this code.
String imageurl = "YOUR URL";
InputStream in = null;
try
{
Log.i("URL", imageurl);
URL url = new URL(imageurl);
URLConnection urlConn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.connect();
in = httpConn.getInputStream();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
Bitmap bmpimg = BitmapFactory.decodeStream(in);
ImageView iv = "YOUR IMAGE VIEW";
iv.setImageBitmap(bmpimg);
Use background thread to get image and after getting image set it in imageview using hanndler.
new Thread(){
public void run() {
try {
Bitmap bitmap = BitmapFactory.decodeStream(new URL("http://imageurl").openStream());
Message msg = new Message();
msg.obj = bitmap;
imageHandler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
Handler code where we set downloaded image in imgaeview.
Handler imageHandler = new Handler(){
public void handleMessage(Message msg) {
if(msg.obj!=null && msg.obj instanceof Bitmap){
imageview.setBackgroundDrawable(new BitmapDrawable((Bitmap)msg.obj));
}
};
};
And ofcourse you need internet permission.
<uses-permission android:name="android.permission.INTERNET" />
You need to set usage permission of INTERNET in android manifest file and use java.net.URL and java.net.URLConnection classes to request the URL.
There is the BitmapFactory-class which can do that. It can create a Bitmap-object from an InputStream, which can then be displayed in an ImageView.
Something you should know is, that using a normal URLConnection to get an InputStream on a URL-object does not always work with the bitmap-factory. A work-around is presented here: Android: Bug with ThreadSafeClientConnManager downloading images
look this:
Option A:
public static Bitmap getBitmap(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
bis.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bm;
}
Option B:
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();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
input.close();
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Jus run the method in a background thread.

Show Avatar Facebook to ImageView

I used the code as follows to show up facebook avatar to ImageView
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.imgAvatar);
img.setImageBitmap(getBitmapFromURL("http://graph.facebook.com/"+"100002394015528"+"/picture"));
}
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;
}
}`
But does it not work. Please help me.
http://graph.facebook.com/id/picture doesn't return an image. It returns some response headers including a 302 redirect, and a location header.
Your example for instance redirects to: http://profile.ak.fbcdn.net/hprofile-ak-snc4/211619_100002394015528_568817_q.jpg
So instead of
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
You need to get the headers from the request, follow the location and then do what you were doing before. I don't know Android, or what language that is. (Java?) So I can't help with that, but I think this might be enough information to get you headed in the right direction.
Use this function for get real URL to avatar:
public static String getUrlFacebookUserAvatar(String name_or_idUser )
{
String address = "http://graph.facebook.com/"+name_or_idUser+"/picture";
URL url;
String newLocation = null;
try {
url = new URL(address);
HttpURLConnection.setFollowRedirects(false); //Do _not_ follow redirects!
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
newLocation = connection.getHeaderField("Location");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newLocation;
}

Android imageview only accepts some jpg's

I have an image view in my app. when I try to make it show a jpg file exported from 3ds max, it works. But if it comes from Photoshop, it just does nothing. Why is that? If it is at all important my app gets the image from my server with the following code:
public static Bitmap getWebImage(String URL)
{
URL myImageURL = null;
Bitmap bitmap = null;
try {
myImageURL = new URL(URL);
} catch (MalformedURLException error) {
error.printStackTrace();
}
try {
HttpURLConnection connection = (HttpURLConnection)myImageURL .openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
Can you check the color settings of your JPG files? Most likely your JPG files from photoshop is in CMYK rather than RGB, and Android simply doesn't support CMYK.
Would it be possible for you to upload the two pictures for comparison?
public static Bitmap getWebImage(String URL)
{
URL myImageURL = null;
Bitmap bitmap = null;
try {
myImageURL = new URL(URL);
} catch (MalformedURLException error) {
error.printStackTrace();
}
try {
HttpURLConnection connection = (HttpURLConnection)myImageURL .openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}

Categories

Resources