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;
}
Related
I am getting a nullPointerException when trying to download a jpg file. It is done in an AsyncTask method and I can't trace the program flow in the debugger probably because it is
asynchronous. My trace reveals that 2 records were read before it stopped. I am using port 8000 as my local server and the url it stops on is
http://10.0.2.2:8000/my_album/5_irises.jpg".
Is there something special about downloading jpegs versus png files or is my url coded incorrectly? Is the underscore a problem in the url? Also, do I have to close the connection after each download?
begin of loop {
........
new AccessImages().execute(urlstring);
} ......end of loop
private class AccessImages extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urladds){
return downloadImage(urladds[0]);
}
protected void onPostExecute(Bitmap bm) {
bitmap_photo[itemcount] = bm;
itemcount++;
}
}
private Bitmap downloadImage(String url) {
Log.d("downloadImage", url);
Bitmap bmap = null;
InputStream inStream = null;
// Drawable drawable = null;
try {
inStream = openHttpConnection(url);
Log.d("inStream", String.valueOf(inStream));
// drawable = Drawable.createFromStream(inStream, "src");
Log.d("before bmap", url);
bmap = BitmapFactory.decodeStream(inStream);
Log.d("after bmap", url);
inStream.close();
}
catch (IOException el) {
el.printStackTrace();
}
return bmap;
}
private InputStream openHttpConnection(String urlString) throws IOException {
InputStream inStream = null;
int checkConn = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
try {
Log.d("try openhttpconnection", urlString);
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
checkConn = httpConn.getResponseCode();
if (checkConn == HttpURLConnection.HTTP_OK) {
inStream = httpConn.getInputStream();
Log.d("instream", urlString);
}
}
catch (Exception ex) {
throw new IOException("Error connecting");
}
return inStream;
}
I just found out the NullPointerException was at this instruction:
inStream.close();
What would cause that?
Ok. Now I corrected for the inStream not being null but now I am getting NullPointerException from the following instruction: bitmap_photo[itemcount] = bm;
if(bm != null)
{
bitmap_photo[itemcount] = bm;
itemcount++;
}
Can't I check for a null value in a bitmap or is the array the problem? I should add that I created the bitmap_photo array as follows: Is this a problem?
Bitmap [] bitmap_photo;
I am trying to set image on imageview but image is not show.
I am reading image url from json data and then trying to set it on ImageView but my image is not visible. No any exception occur.
Here is my code
HotelList.class
static final String TAG_DISHIMAGEURL = "dishimageurl";
......
String imageUrl = dishResult.getString(TAG_DISHIMAGEURL);
map.put(TAG_DISHIMAGEURL, imageUrl);
.....
dishimageurl1 = hm.get(TAG_DISHIMAGEURL).toString();
intent.putExtra("background", dishimageurl1);
HotelDetails.class
......
String dishimageurl = bundle.getString("background");
Bitmap bimage= getBitmapFromURL(dishimageurl);
imageView.setImageBitmap(bimage);
....
public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
Toast.makeText(this, "showing image", Toast.LENGTH_LONG).show();
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
Toast.makeText(this, "showing exception", Toast.LENGTH_LONG).show();
return null;
}
}
I don't understand what happen with this code. No any exception but my image is not visible.
Please give me any reference.
Please Use below code for get image from url and display into imageview.
public class image extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
RelativeLayout mRlayout1 = (RelativeLayout) findViewById(R.id.mRlayout1);
Drawable d=new BitmapDrawable(bitmap);
mRlayoutLogin.setBackgroundDrawable(d);
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
}
you can view image by using this code.
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
It seems you are downloading the image from UI thread. this will block the UI thread and will give you not responding error. as an easy way, you can use a library like Universal Image Loader
Universal Image Loader - GitHub
this will manage the image loading for you and avoid problems like incorrect urls, Out Of Memory error.
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
I have this code from a book I have to learn about Android .. what's wrong?
I always get 01 Error Connecting which is an exception in my code while establishing http connection.
public class HttpImgActivity extends Activity {
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null; // creating My input
int response = -1;
URL url= new URL(urlString);
URLConnection conn = url.openConnection();
if(!(conn instanceof HttpURLConnection)) // if not a valid URL
throw new IOException ("NOT an Http connection");
try{
HttpURLConnection httpconn = (HttpURLConnection) conn;
httpconn.setAllowUserInteraction(false); // prevent user interaction
httpconn.setInstanceFollowRedirects(true);
httpconn.setRequestMethod("GET");
httpconn.connect(); //initiates the connection after setting the connection properties
response = httpconn.getResponseCode(); // getting the server response
if(response == HttpURLConnection.HTTP_OK ) // if the server response is OK then we start receiving input stream
{ in = httpconn.getInputStream(); }
} // end of try
catch(Exception ex)
{
throw new IOException(" 01 Error Connecting");
}
return in; // would be null if there is a connection error
} // end of my OpenHttpConnection user defined method
*/
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap= null;
InputStream in = null;
try
{
in = getInputStreamFromUrl(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
}
catch (IOException e1)
{
Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
return bitmap; // this method returns the bitmap which is actually the image itself
}
ImageView img;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = DownloadImage("http://www.egyphone.com/wp-content/uploads/2011/05/Samsung_Galaxy_S_II_2.jpg");
img =(ImageView) findViewById(R.id.myImg);
img.setImageBitmap(bitmap);
}
}
Any ideas?
It seems you catch your exception, but you don't use it for anything.
Try changing throw new IOException(" 01 Error Connecting"); to throw new IOException(ex.toString());
And you should think about using Android's logging tools, instead to see your errors through logcat:
...
catch(Exception ex)
{
Log.e("CONNECTION", ex.toString(), ex);
}
...
This makes debugging easier IMO.
I want to read an image from a this URL photo
and I'm using the following code
public static Bitmap DownloadImage(String URL)
{
Bitmap bitmap=null;
InputStream in=null;
try {
in=networking.OpenHttpConnection("http://izwaj.com/"+URL);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=8;
bitmap=BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (Exception e) {
// TODO: handle exception
}
return bitmap;
}
public class Networking {
public InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in=null;
int response = -1;
URL url=new URL(urlString);
URLConnection conn=url.openConnection();
if(!(conn instanceof HttpURLConnection))
{
throw new IOException("Not an HTTP connection");
}
try {
HttpURLConnection httpconn=(HttpURLConnection)conn;
httpconn.setAllowUserInteraction(false);
httpconn.setInstanceFollowRedirects(true);
httpconn.setRequestMethod("GET");
httpconn.connect();
response=httpconn.getResponseCode();
if(response==HttpURLConnection.HTTP_OK)
{
in=httpconn.getInputStream();
}
}
catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
The bitmap always returned to me null.. I used also other functions found on the internet. all returned the bitmap as null value :S
use this function it may help you.
public Bitmap convertImage(String url)
{
URL aURL = null;
try
{
final String imageUrl =url.replaceAll(" ","%20");
Log.e("Image Url",imageUrl);
aURL = new URL(imageUrl);
URLConnection conn = aURL.openConnection();
InputStream is = conn.getInputStream();
//#SuppressWarnings("unused")
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(new PatchInputStream(bis));
if(bm==null)
{}
else
Bitmap bit=Bitmap.createScaledBitmap(bm,72, 72, true);//mention size here
return bit;
}
catch (IOException e)
{
Log.e("error in bitmap",e.getMessage());
return null;
}
}
I would check the %20 spacing on the picture
http://184.173.7.132/RealAds_Images/Apartment%20for%20sale,%20Sheikh%20Zayed%20_%201.jpg
To me the %20 will not render a space so ensure this is noted in your code
if you change the file to apartmentforsalesheikhzayed20201.jpg or something like as a test apartment.jpg it will work.
Personally instead of using spaces in your image names i would use an underscore between the spaces so no other code is needed try_renaming_your_photo_to_this.jpg :)