I’m working on some code which fills a GridView full of images that have been pulled from YouTube. Now its working apart from when it can’t find an image, I’m not sure why it can’t find the image as the image link works in a browser but this actually works to my advantage as it weeds out videos that have been removed due to copyright/account closure etc.
My code to get the image is as follows
Call
final Object item = allMatches.get(position);
if(item != null) {
Bitmap bitmap = loadBitmap("http://i2.ytimg.com/vi/"+ allMatches.get(position).toString() + "/default.jpg");
imageView.setImageBitmap(bitmap);
}
else { imageView.setImageDrawable(getResources().getDrawable(R.drawable.default1)); }
loadBitmap()
public static Bitmap loadBitmap(String url)
{
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), 4 * 1024);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, 4 * 1024);
int byte_;
while ((byte_ = in.read()) != -1)
out.write(byte_);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e("","Could not load Bitmap from: " + url);
} finally {
try{
in.close();
out.close();
}catch( IOException e )
{
System.out.println(e);
}
}
return bitmap;
So this will sometimes crash with the following Stacktrace
03-23 01:47:10.087: E/(9757): Could not load Bitmap from: http://i2.ytimg.com/vi/8s3lnGuyC9M/default.jpg
03-23 01:47:10.247: W/dalvikvm(9757): threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-23 01:47:10.267: E/AndroidRuntime(9757): FATAL EXCEPTION: main
03-23 01:47:10.267: E/AndroidRuntime(9757): java.lang.NullPointerException
03-23 01:47:10.267: E/AndroidRuntime(9757): at com.myapp.Sample.loadBitmap(Sample.java:159)
03-23 01:47:10.267: E/AndroidRuntime(9757): at com.myapp..Sample$ImageAdapter.getView(Sample.java:207)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.widget.AbsListView.obtainView(AbsListView.java:1467)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.widget.GridView.onMeasure(GridView.java:935)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.view.View.measure(View.java:8330)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.view.View.measure(View.java:8330)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.view.View.measure(View.java:8330)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.view.View.measure(View.java:8330)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.view.View.measure(View.java:8330)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.os.Looper.loop(Looper.java:130)
03-23 01:47:10.267: E/AndroidRuntime(9757): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-23 01:47:10.267: E/AndroidRuntime(9757): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 01:47:10.267: E/AndroidRuntime(9757): at java.lang.reflect.Method.invoke(Method.java:507)
03-23 01:47:10.267: E/AndroidRuntime(9757): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-23 01:47:10.267: E/AndroidRuntime(9757): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-23 01:47:10.267: E/AndroidRuntime(9757): at dalvik.system.NativeStart.main(Native Method)
What I need to do is just be able to deal with it not being able to retrieve the image. My Java skills are limited but I want something like the below (but actually working)
Bitmap bitmap = loadBitmap("http://i2.ytimg.com/vi/"+ allMatches.get(position).toString() + "/default.jpg"); //
if (bitmap == null) {
imageView.setImageBitmap(defaultBitmap);
} else {
imageView.setImageBitmap(bitmap); }
or
//Doesn't work anyway due to 'Exception IOException is not compatible with throws clause in Adapter.getView'
try {
Bitmap bitmap = loadBitmap("http://i2.ytimg.com/vi/"+ allMatches.get(position).toString() + "/default.jpg");
imageView.setImageBitmap(bitmap);
}catch( IOException ) {
imageView.setImageBitmap(defaultBitmap);
}
My understanding of try catch blocks is that if you ‘catch the error’ the code should continue. In this case shouldn’t it just be printing the error to the log and carrying on?
How can I do this for when ‘loadBitmap’ fails at retrieving the image?
EDIT: I have updated the code as per some user comments – it still provides the exact same error, it just removes some doubt around the point of failure. Also, to clarify
Line 159: in.close();
Line 207: Bitmap bitmap = loadBitmap("http://i2.ytimg.com/vi/"+ allMatches.get(position).toString() + "/default.jpg");
First:
Second: