简体   繁体   中英

NullPointerException when setting bitmap in imageview

Hi all i'm getting a NPE when setting an ImageView a bitmap. The bitmap has been decoded from a bytearray and is not null or empty. any ideas? thanks

public class LoadPic extends Activity{

    private static final String TAG = "Loadpic";
    private ImageView imageview;
    private File tempFile;
    private byte[] imageArray;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Log.e(TAG, " loadpic onCreate");
        setContentView(R.layout.load);

        imageview = (ImageView)findViewById(R.id.imageView1);

        tempFile = new File(Environment.getExternalStorageDirectory().
             getAbsolutePath() + "/"+"image.jpg");

        imageArray = new byte[(int)tempFile.length()];

  try{

        InputStream is = new FileInputStream(tempFile);
        BufferedInputStream bis = new BufferedInputStream(is);
        DataInputStream dis = new DataInputStream(bis);


        int i = 0;

        while (dis.available() > 0) {
        imageArray[i] = dis.readByte();
        i++;
        }

        dis.close();

  } catch (Exception e) {

           e.printStackTrace();
        }

        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 5;
        Bitmap bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo);
        Log.e(TAG, bm.toString());
        imageview.setImageBitmap(bm);

    }// end of onCreate


}//end of Activity








<?xml version="1.0" encoding="UTF-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical" android:layout_width="match_parent" 
                    android:layout_height="wrap_content" android:gravity="fill">


        <ImageView android:id="@+id/imageView1" 
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"></ImageView>


     </RelativeLayout>               

确保具有android:id="@+id/imageView1" res/layout/load.xml android:id="@+id/imageView1"ImageView实际上出现在res/layout/load.xml

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM