gpt4 book ai didi

java - 在目录android中保存图像时出现空指针异常

转载 作者:行者123 更新时间:2023-11-30 01:42:00 25 4
gpt4 key购买 nike

<分区>

我尝试了以下代码,其中我试图将我上传的图像保存到具有某个名称的 SD 卡文件夹中。它在提到的行中给出了空指针异常。请有任何建议。

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference

代码

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();
}
}

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