如果从图库中选择图像或通过相机使我的应用程序崩溃捕获图像,id会做什么。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
bitmap = getResizedBitmap(bitmap, 400);
img_cam.setImageBitmap(bitmap);
img_cam.setVisibility(View.VISIBLE);
BitMapToString(bitmap);
String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path,String.valueOf(System.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} catch (Exception e) { e.printStackTrace(); }
} catch (Exception e) { e.printStackTrace(); }
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getActivity().getContentResolver().query(selectedImage,filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
thumbnail = getResizedBitmap(thumbnail, 400);
Log.w("path of image from gallery......******************.........", picturePath+"");
img_cam.setImageBitmap(thumbnail);
img_cam.setVisibility(View.VISIBLE);
BitMapToString(thumbnail);
}
}
}
public String BitMapToString(Bitmap userImage1) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
userImage1.compress(Bitmap.CompressFormat.PNG, 60, baos);
byte[] b = baos.toByteArray();
Document_img1 = Base64.encodeToString(b, Base64.DEFAULT);
return Document_img1;
}
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at package.name.Fragment.ChatFragment.getResizedBitmap(ChatFragment.java:414)
at package.name.Fragment.ChatFragment.onActivityResult(ChatFragment.java:394)