【发布时间】:2015-12-19 10:09:44
【问题描述】:
我尝试了以下代码,其中我试图将我上传的图像保存到具有某个名称的 SD 卡文件夹中。它在提到的行上给出了空指针异常。请有任何建议。
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()'
代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==i && resultCode==RESULT_OK && data != null){
Uri selectedImage = data.getData();
coverpic.setImageURI(selectedImage);
Bitmap image =( (BitmapDrawable)coverpic.getDrawable()).getBitmap();//This line throws the exception
if (!direct.exists()) {
File wallpaperDirectory = new File("/images/");
wallpaperDirectory.mkdirs();
}
Bundle extras = getIntent().getExtras();
File file = new File(new File("/images/"),extras.getString("name")+i + ".jpg" );
if (file.exists()) {
file.delete();
}
try{
FileOutputStream out=new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG,100,out);
out.flush();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
【问题讨论】:
-
@bhargav 它是一个上传的图片变量
-
@gvmani 我该如何解决这个问题
-
@AnoshaKhan 你确定
selectedImage不为空吗?
标签: java android bitmap io nullpointerexception