Android crashing after camera Intent

后端 未结 8 1415
半阙折子戏
半阙折子戏 2020-12-04 15:13

I have an app published and one of the fundamental features is to allow the user to take a picture, and then save that photo in a specific folder on their External Storage.

8条回答
  •  难免孤独
    2020-12-04 15:31

    First lets make it clear - we have two options to take image data in onActivityResult from Camera:

    1. Start Camera by passing the exact location Uri of image where you want to save.

    2. Just Start Camera don't pass any Loaction Uri.


    1 . IN FIRST CASE :

    Start Camera by passing image Uri where you want to save:

    String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";  
    File imageFile = new File(imageFilePath); 
    Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri
    
    Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
    startActivityForResult(it, CAMERA_RESULT);
    

    In onActivityResult receive the image as:

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
    
        if (RESULT_OK == resultCode) { 
            iv = (ImageView) findViewById(R.id.ReturnedImageView); 
    
            // Decode it for real 
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = false; 
    
            //imageFilePath image path which you pass with intent 
            Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions); 
    
            // Display it 
            iv.setImageBitmap(bmp); 
        }    
    } 
    

    2 . IN SECOND CASE:

    Start Camera without passing image Uri, if you want to receive image in Intent(data) :

    Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(it, CAMERA_RESULT); 
    

    In onActivityResult recive image as:

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
    
        if (RESULT_OK == resultCode) { 
            // Get Extra from the intent 
            Bundle extras = data.getExtras(); 
            // Get the returned image from extra 
            Bitmap bmp = (Bitmap) extras.get("data"); 
    
            iv = (ImageView) findViewById(R.id.ReturnedImageView); 
            iv.setImageBitmap(bmp); 
        } 
    } 
    


    *****Happy Coding!!!!*****

提交回复
热议问题