我试过以下代码,其中我试图将我上传的图像保存到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();
}
}
答案 0 :(得分:1)
试试这个,这会调整大小并将图像保存到手机
还要确保您在清单文件中拥有正确的权限。
这里我也使用Glide来获取位图,我认为这是一种简单的方法,它不会给出nullpointer。
在onActivityresult内部
if(requestCode==i && resultCode==RESULT_OK && data != null){
Uri selectedImage = data.getData();
select(selectedImage);
查找文件
public void select(Uri selectedImage) {
// TODO Auto-generated method stub
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor =getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath2 = cursor.getString(columnIndex);
cursor.close();
save(new File(picturePath2));
}
保存文件
public File save(final File file_) {
// TODO Auto-generated method stub
System.out.println("newfilepath come to resize");
File dir = new File(Environment.getExternalStorageDirectory().getPath()
+ "/Images/");
try {
dir.mkdir();
} catch (Exception e) {
e.printStackTrace();
}
String filename = Environment.getExternalStorageDirectory().getPath()
+ "/Images/";
File newfile = new File(filename);
String Unedited_Img_Name = "myfile"
+ String.valueOf(System.currentTimeMillis()) + ".jpg";
final File file = new File(newfile, Unedited_Img_Name);
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
// TODO Auto-generated method stub
Bitmap b = null;
try {
b = Glide.with(getActivity()).load(file_).asBitmap().into(200,200)
.get();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
b.recycle();
} catch (Exception e) { // TODO
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
String newfilepath = file.getAbsolutePath();
int file_size = Integer
.parseInt(String.valueOf(file.length() / 1024));
file_size = Integer.parseInt(String.valueOf(file_.length() / 1024));
if (file_size < 150) {
newfilepath = file_.getAbsolutePath();
}
}
}.execute();
return file;
}