我试图在从默认相机拍摄照片后保存我的照片并将其存储到服务器中。
但是在我成功之后将位图设置为imageview,然后按下按钮将照片存储到服务器中,出现错误:
尝试在空对象引用上调用虚方法'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,int,java.io.OutputStream)'
这是我的整体代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
cmdSelesai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bm = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, bao); <--- This line is error
byte[] ba = bao.toByteArray();
ba1 = Base64.encodeToString(ba, Base64.NO_WRAP);
new uploadToServer().execute();
}
});
OnActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1888 && data != null)
{
selectedImage = data.getData();
photo = (Bitmap) data.getExtras().get("data");
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
imageView.setImageBitmap(photo);
}
else if (data == null)
{
Toast.makeText(getApplicationContext(), "Batal mengambil gambar", Toast.LENGTH_SHORT).show();
}
else
{
Bitmap bp = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bp);
}
}
OnSaveInstance和OnRestoreInstance:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("order", order);
savedInstanceState.putString("shipment", shipment);
savedInstanceState.putString("penerima", penerima);
savedInstanceState.putString("kode_driver", kode_driver);
savedInstanceState.putString("picturePath", picturePath);
savedInstanceState.putStringArray("kode_produk", kode_prdct_temp);
savedInstanceState.putParcelable("selectedImage", selectedImage);
savedInstanceState.putParcelable("bm", photo);
savedInstanceState.putParcelable("photo", photo);
savedInstanceState.putSerializable("myMap2", (Serializable) myMap2);
savedInstanceState.putSerializable("mapAlasanReal", (Serializable) mapAlasanReal);
// etc.
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
order = savedInstanceState.getString("order");
kode_driver = savedInstanceState.getString("koder_driver");
shipment = savedInstanceState.getString("shipment");
penerima = savedInstanceState.getString("penerima");
picturePath = savedInstanceState.getString("picturePath");
bm = savedInstanceState.getParcelable("bm");
photo = savedInstanceState.getParcelable("photo");
kode_prdct_temp = savedInstanceState.getStringArray("kode_produk");
selectedImage = savedInstanceState.getParcelable("selectedImage");
myMap2 = (Map<Integer, Integer>) savedInstanceState.getSerializable("myMap2");
mapAlasanReal = (Map<Integer, Integer>) savedInstanceState.getSerializable("mapAlasanReal");
}
请帮助我!
已编辑:
这是我的权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />