gpt4 book ai didi

android - 在 imageview 中设置位图时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 15:32:10 27 4
gpt4 key购买 nike

大家好,我在将 ImageView 设置为位图时遇到了 NPE。位图已从字节数组解码,并且不为 null 或空。有任何想法吗?谢谢

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"ImageView 实际上出现在 res/layout/load.xml.

关于android - 在 imageview 中设置位图时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5300456/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com