简体   繁体   English

无法将意图用于CAMERA

[英]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. 在我的应用程序中,单击“图像视图”后,我要启动Camera,它将捕获图像并将其显示在另一个图像视图中。
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. 例如:如果我正在使用带有棒棒糖的手机Yu Yureka来测试我的应用程序,它将给我的数据为null。此外,当方向更改时,应用程序崩溃了。请帮助我解决此问题。

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();
            }


        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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