I am trying to write a code that downloads a picture from a url, and put it in ImageView.
The Class that downloads it, is UrlDoanload that returns the raw image in a byte array.
the Main Activity uses a AsyncTask inner private class to call UrlDoanload. in the onPostExecute method it puts the byte array in a ImageView, by using Bitmap, BitmapFactory objects.
the problem is, that the Bitmap object becomes null.
this is the code:
mainActivity:
package com.example.downloadingimageandpuingrid;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
public String url;
public Byte [] imageBytes;
public ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_item);
imageView = (ImageView) findViewById(R.layout.gallery_item);
url = "http://www.royalcanin.in/var/royalcanin/storage/images/breeds/cat-breeds/norwegian-forest-cat/19311843-15-eng-GB/norwegian-forest-cat_cat_breed_cat_picture.jpg";
new FetchItemsTask().execute();
}
private class FetchItemsTask extends AsyncTask<Void,Void, byte[] > {
@Override
protected byte[] doInBackground(Void... params) {
return new UrlDownload().getUrlBytes(url);
}
@Override
protected void onPostExecute(byte[] imageBytes) {
final Bitmap bitmap = BitmapFactory
.decodeByteArray(imageBytes, 0, imageBytes.length);
imageView.setImageBitmap(bitmap);
}
}
}
urlDownload:
package com.example.downloadingimageandpuingrid;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.util.Log;
public class UrlDownload {
byte[] getUrlBytes(String urlSpec)
{
try{
return download(urlSpec);
} catch(Exception e){
//e.printStackTrace();
Log.e("UrlDownload", "error downloading");
byte[] empety = {};
return empety;
}
}
public byte[] download (String urlSpec) throws IOException
{
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
{
return null;
}
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0)
{
out.write(buffer, 0, bytesRead);
}
out.close();
return out.toByteArray();
}
finally {
connection.disconnect();
}
}
}
gallary_item layout:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery_item_imageView"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:scaleType="centerCrop" >
</ImageView>
this is the logCat:
11-23 15:10:44.348: D/libEGL(3964): loaded /system/lib/egl/libEGL_genymotion.so
11-23 15:10:44.352: D/(3964): HostConnection::get() New Host Connection established 0xb9259888, tid 3964
11-23 15:10:44.364: D/libEGL(3964): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
11-23 15:10:44.364: D/libEGL(3964): loaded /system/lib/egl/libGLESv2_genymotion.so
11-23 15:10:44.436: W/EGL_genymotion(3964): eglSurfaceAttrib not implemented
11-23 15:10:44.436: E/OpenGLRenderer(3964): Getting MAX_TEXTURE_SIZE from GradienCache
11-23 15:10:44.452: E/OpenGLRenderer(3964): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
11-23 15:10:44.452: D/OpenGLRenderer(3964): Enabling debug mode 0
11-23 15:10:44.684: D/skia(3964): --- SkImageDecoder::Factory returned null
11-23 15:10:44.684: D/AndroidRuntime(3964): Shutting down VM
11-23 15:10:44.684: W/dalvikvm(3964): threadid=1: thread exiting with uncaught exception (group=0xa4b84648)
11-23 15:10:44.684: E/AndroidRuntime(3964): FATAL EXCEPTION: main
11-23 15:10:44.684: E/AndroidRuntime(3964): java.lang.NullPointerException
11-23 15:10:44.684: E/AndroidRuntime(3964): at com.example.downloadingimageandpuingrid.MainActivity$FetchItemsTask.onPostExecute(MainActivity.java:36)
11-23 15:10:44.684: E/AndroidRuntime(3964): at com.example.downloadingimageandpuingrid.MainActivity$FetchItemsTask.onPostExecute(MainActivity.java:1)
11-23 15:10:44.684: E/AndroidRuntime(3964): at android.os.AsyncTask.finish(AsyncTask.java:631)
11-23 15:10:44.684: E/AndroidRuntime(3964): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-23 15:10:44.684: E/AndroidRuntime(3964): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-23 15:10:44.684: E/AndroidRuntime(3964): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 15:10:44.684: E/AndroidRuntime(3964): at android.os.Looper.loop(Looper.java:137)
11-23 15:10:44.684: E/AndroidRuntime(3964): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-23 15:10:44.684: E/AndroidRuntime(3964): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 15:10:44.684: E/AndroidRuntime(3964): at java.lang.reflect.Method.invoke(Method.java:525)
11-23 15:10:44.684: E/AndroidRuntime(3964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-23 15:10:44.684: E/AndroidRuntime(3964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 15:10:44.684: E/AndroidRuntime(3964): at dalvik.system.NativeStart.main(Native Method)
thanks!