从相机或图库返回时,我的应用程序崩溃而不选择图像。但是,如果您选择图像或拍照,应用程序不会崩溃。
相机工作正常,但如果您尝试返回屏幕而不拍照,应用程序将崩溃。与图库功能相同。
如果有人对解决此问题有任何建议,请随时回复
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode== CAMERA_PERM_CODE){
if (grantResults.length < 0 && grantResults [0] == PackageManager.PERMISSION_GRANTED){
// openCamera() ;
}else {
Toast.makeText(this, "Camera Permission is Required to use Camera", Toast.LENGTH_SHORT).show();
}
}
}
private void askCameraPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.CAMERA}, CAMERA_PERM_CODE);
}else {
opencamera();
}
}
private void opencamera() {
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera,CAMERA_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE) {
Bitmap image = (Bitmap) data.getExtras().get("data");
SelectedImage.setImageBitmap(image);
}
else if (requestCode == GALLERY_REQUEST_CODE) {
Uri Imageuri = data.getData();
SelectedImage.setImageURI(Imageuri);
}
}
private File saveBitMap(Context context, View drawView){
FileOutputStream outputStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File pictureFileDir = new File(sdCard.getAbsoluteFile()+"/test");
pictureFileDir.mkdir();
String filename = pictureFileDir.getPath() +File.separator+ System.currentTimeMillis()+".jpg";
File pictureFile = new File(filename);
Bitmap bitmap =getBitmapFromView(drawView);
try {
pictureFile.createNewFile();
FileOutputStream oStream = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, oStream);
try {
oStream.flush();
}
catch (Exception e){
e.printStackTrace();
}
oStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.i("TAG", "There was an issue saving the image.");
}
scanGallery( context,pictureFile.getAbsolutePath());
return pictureFile;
}
//create bitmap from view and returns it
private Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
} else{
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.TRANSPARENT);
}
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
// used for scanning gallery
private void scanGallery(Context cntx, String path) {
try {
MediaScannerConnection.scanFile(cntx, new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
} catch (Exception e) {
e.printStackTrace();
Log.i("TAG", "There was an issue scanning gallery.");
}
}
}
答案 0 :(得分:0)
检查 onActivityResult()
中的结果是否收到,因此请使用 RESULT_OK
进行检查。当您返回没有选择图像时,您的数据为空
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
Bitmap image = (Bitmap) data.getExtras().get("data");
SelectedImage.setImageBitmap(image);
}
else if (requestCode == GALLERY_REQUEST_CODE && resultCode = RESULT_OK) {
Uri Imageuri = data.getData();
SelectedImage.setImageURI(Imageuri);
}
}