【发布时间】:2014-01-30 17:23:34
【问题描述】:
我正在尝试从图库中选择图像并将其设置为 listview 中的 imageview,因为我正在编写一段代码。 此代码抛出 NULL POINTER EXCEPTION ,我无法解决该错误。在这种情况下请帮助我
ContactInfoMoreOption.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==GALLERY_REQUEST) {
if (resultCode==RESULT_OK) {
View view=getLayoutInflater().inflate(R.layout.list_row,null);
ImageView imgView=(ImageView)view.findViewById(R.id.list_image);
InputStream is = null;
try {
is = getContentResolver().openInputStream(data.getData());
} catch (Exception e) {
e.printStackTrace();
}
Bitmap bitmap=BirthdayCalculation.getThumbnailBitmap(is, 200);
byte bitObj[]=BirthdayCalculation.convertImageToByte(bitmap);
ContentValues values=new ContentValues();
values.put(BirthdayProvider.PHOTO, bitObj);
int count=getContentResolver().update(BirthdayProvider.CONTENT_URI, values, BirthdayProvider.NUMBER+"='"+SearchListActivity.longClickValue+"'", null);
if (count==1) {
finish();
imgView.setImageBitmap(bitmap);
imgView.setScaleType(ScaleType.FIT_XY);
Log.v("Photo Updated Successfully", "Photo Updated Successfully");
Toast.makeText(getBaseContext(),"Updated Successfully",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getBaseContext(),"Updation Failed",Toast.LENGTH_SHORT).show();
}
}
}
}
生日计算.java
public static byte[] convertImageToByte(Bitmap bitmap){
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG,0,outputStream);
return outputStream.toByteArray();
}
public static Bitmap getThumbnailBitmap(InputStream is, final int thumbnailSize) {
Bitmap bitmap;
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is,null, bounds);
if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
bitmap = null;
}
int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
: bounds.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / thumbnailSize;
bitmap = BitmapFactory.decodeStream(is, null, opts);
return bitmap;
}
错误
java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=1, result=-1, data=Intent {
dat=content://media/external/images/media/12532 }} to activity
{com.android.project.birthdayreminder/com.android.project.birthdayreminder.ContactInfoMoreOption}:
java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at com.android.project.birthdayreminder.BirthdayCalculation.convertImageToByte(BirthdayCalculation.java:565)
at com.android.project.birthdayreminder.ContactInfoMoreOption.onActivityResult(ContactInfoMoreOption.java:720)
at android.app.Activity.dispatchActivityResult(Activity.java:5192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3145)
【问题讨论】:
-
请发布完整的logcat。
-
您在哪一行收到此错误??
-
贴了logcat代码,请看一下
-
@vipul mittal --- 位图 bitmap=BirthdayCalculation.getThumbnailBitmap(is, 200); byte bitObj[]=BirthdayCalculation.convertImageToByte(bitmap);
-
@karthik 检查我的回答。让我知道它是否适合您。
标签: android