I'm trying to download an image from a URL and convert it into a bitmap, but the line
Bitmap myBitmap = BitmapFactory.decodeStream(input);
always causes the debugger to skip to the following line
return null;
without ever actually printing out the stack trace and the Exception variable also doesn't exist in the variables listed in the Debugger. I read a lot about how there might be issues with urls not actually leading to images, not well formated images and the like but it still has the same issue with a hardcoded image that I'm positive exists.
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(
"http://www.helpinghomelesscats.com/images/cat1.jpg");
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;
}
}
Since it seems like the manifest.xml file might be involved I've edited to add here.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.delivery"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".IntroPage"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Browse"></activity>
<activity android:name=".ViewProduct"></activity>
<activity android:name=".ViewOrder"></activity>
<activity android:name=".GetAddress"></activity>
<activity android:name=".ConfirmOrder"></activity>
</application>
following code work for all type of images.
try {
URL url = new URL("http://www.helpinghomelesscats.com/images/cat1.jpg");
InputStream in = url.openConnection().getInputStream();
BufferedInputStream bis = new BufferedInputStream(in,1024*8);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len=0;
byte[] buffer = new byte[1024];
while((len = bis.read(buffer)) != -1){
out.write(buffer, 0, len);
}
out.close();
bis.close();
byte[] data = out.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bitmap);
}
catch (IOException e) {
e.printStackTrace();
}
Hi you may give these 2 pemssions in the android manifeast file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
In your activity:
url="http://www.helpinghomelesscats.com/images/cat1.jpg"
Bitmap bmp=readBitmapFromNetwork(url);
public static Bitmap readBitmapFromNetwork(String imgurl) {
URL url;
Bitmap bmp = null;
InputStream is = null;
BufferedInputStream bis = null;
System.out.println("image url ========== "+imgurl);
try {
url=new URL(imgurl);
System.out.println("url.getPath()"+url.getPath());
try {
URLConnection conn = url.openConnection();
conn.connect();
is = conn.getInputStream();
bis = new BufferedInputStream(is);
bmp = BitmapFactory.decodeStream(bis);
} catch (MalformedURLException e) {
System.out.println("Bad ad URL");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Could not get remote ad image");
e.printStackTrace();
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally {
try {
if( is != null )
is.close();
if( bis != null )
bis.close();
} catch (IOException e) {
System.out.println("Error closing stream.");
e.printStackTrace();
}
}
return bmp;
}
This works for me, found it on here while searching out another issue. This way it's not holding up the UI. works very well in ArrayAdapters as well.
Useage:
_coverArt = (ImageView) row.findViewById(R.id.top_CoverArt);
new DownloadImageTask(_coverArt)
.execute("Put your URL here Hint: make sure it's valid http://www.blah...");
The Asynctask
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.