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>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…