简体   繁体   中英

Unable to use intent for CAMERA

In my application ,on clicking Image View i want to start Camera which will capture image and display it in another image view.
My code is given below:

 //On clicking Camera
    iv_camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        if (data != null) {
            Log.e("Result Code", String.valueOf(resultCode));
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            Uri selectedImageUri = data.getData();
            Log.e("ImageUri", String.valueOf(selectedImageUri));
            String realPath = getRealPathFromURI(selectedImageUri);
             Log.e("Real Path", realPath);
            imgProfilePic.setImageBitmap(photo);
        }
    }
}

//Get real path form Uri
public String getRealPathFromURI(Uri contentUri) {
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } catch (Exception e) {
        return contentUri.getPath();
    }
}

My problem is that ,on some mobiles it is working fine but on some it is not.
For example: If i am testing my application using on my phone Yu Yureka having Lollipop ,it is giving me data as null.Also when the orientation changes,application is crashing .Please help me to fix the issue.

In your Manifest file use these permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Implement from this Simple Code Example this is my working example code

public void CameraClick(View v) {

        Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        // creating Dir to save clicked Photo
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/cam_Intent");
        myDir.mkdirs();

        // save clicked pic to given dir with given name

        File file = new File(Environment.getExternalStorageDirectory(),
                "/cam_Intent/MyPhoto.jpg");

        outPutfileUri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
        startActivityForResult(intent, TAKE_PIC);
    }

    Bitmap bitmap = null;

    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent data)
    {
        if (requestCode == TAKE_PIC && resultCode==RESULT_OK) {

            String uri = outPutfileUri.toString();
            Log.e("uri-:", uri);
            Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();

            //Bitmap myBitmap = BitmapFactory.decodeFile(uri);
           // mImageView.setImageURI(Uri.parse(uri));   OR drawable make image strechable so try bleow also

            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), outPutfileUri);
                Drawable d = new BitmapDrawable(getResources(), bitmap);
                mImageView.setImageDrawable(d);
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM