Android - Problem getting image uri

前端 未结 1 1600
借酒劲吻你
借酒劲吻你 2021-01-07 02:45

like the title says my problem concerns the image uri of images taken by the camera. Uri is always null ... WHY?

@Override
public void onClick(View v) {

           


        
1条回答
  •  灰色年华
    2021-01-07 03:12

    Look at this,

    Android ACTION_IMAGE_CAPTURE Intent

    might provide you with some insight.

    public void startCamera() {
    
        File photo = null;
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            photo = new File(android.os.Environment
                    .getExternalStorageDirectory(), FILE_NAME);
        } else {
            photo = new File(getCacheDir(), FILE_NAME);
        }    
        if (photo != null) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
            selectedImageUri = Uri.fromFile(photo);
            startActivityForResult(intent, IMAGE_CAPTURE);
        }
    }
    
    
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
         if (requestCode == IMAGE_CAPTURE) {
    
            if (resultCode == Activity.RESULT_OK) {
    
                try {
                    Uri selectedImage = selectedImageUri;
                    //getContentResolver().notifyChange(selectedImage, null);
                    ContentResolver cr = getContentResolver();
                    Bitmap bitmap;
                    bitmap = android.provider.MediaStore.Images.Media
                            .getBitmap(cr, selectedImage);
                    rptImage.setImageBitmap(bitmap);
    
                } catch (Exception e) {
                    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                            .show();
                    Log.e("Camera", e.toString());
                }
    
            } else {
                selectedImageUri = null;
                rptImage.setImageBitmap(null);
            }
    
        } 
    
    }
    

    That's a code sample from my application.

    You need the permission,

    
        
        
    

    0 讨论(0)
提交回复
热议问题