开发者

Camera Intent result woes

I am attempting to launch the built-in camera to take a picture, a picture that will have a name specified by the activity launching the camera. (code below)

  1. When the camera returns, onActivityResult() goes straight to resultCode == Activity.RESULT_CANCELED. Any explanation for this and solutions would be greatly appreciat开发者_高级运维ed.

  2. The camera indeed does take the image, I can see it in my sdcard with a file viewer, but its name is the stock one from the camera. How can I get the name of this taken image to be the one supplied by the activity?

Camera intent code

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File("Team image.jpg");
camera.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
camera.putExtra(MediaStore.Images.Media.TITLE, "Team image");
        startActivityForResult(camera, PICTURE_RESULT);

activityresult code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){

    if(requestCode == PICTURE_RESULT){
        if(resultCode == Activity.RESULT_OK) {
            if(data!=null){
                Bitmap image = BitmapFactory.decodeFile(data.getExtras().get(MediaStore.Images.Media.TITLE).toString());
                grid.add(image);            
                images.addItem(image);
            }
            if(data==null){
                Toast.makeText(Team_Viewer.this, "no data.", Toast.LENGTH_SHORT).show();
            }
        }
        else if(resultCode == Activity.RESULT_CANCELED) {
            Toast.makeText(Team_Viewer.this, "Picture could not be taken.", Toast.LENGTH_SHORT).show();
        }
}
}


Have you mark the launch mode of your activity as "singleInstance"?

That may cause your first problem.

My camera goes normal when I remove the "singleInstance".


The two issues are likely related, having to do with the way you are creating the file reference that passes to the camera. If you want your image file to save to the SD Card, you need to create a file reference that includes a full-path to that location, not just a filename. For example, this code would save the image file on the SD card root:

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(Environment.getExternalStorageDirectory(),"TeamImage.jpg");
camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));

startActivityForResult(camera, PICTURE_RESULT);

I also changed your filename to not include a space; only because I'm not certain that the Camera application won't blow up on that piece also. Since the Camera is getting confused trying to open and write to your file location, that is likely why you always return with RESULT_CANCELED. You don't need the WRITE_EXTERNAL_STORAGE permission here, since the Camera app is doing the SD Card access.

One more note: I don't believe other MediaStore extras can be passed with this Intent. Typically, if you want metadata to be attached to your image, you have to insert the Uri reference with that metadata into the MediaStore ContentProvider prior to saving the image to disk.

Hope that helps!


Not sure what's wrong with your code, here's what works for me:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);

and

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {

            switch (requestCode) {
                case CAMERA_PIC_REQUEST:
                    Bitmap b = (Bitmap) data.getExtras().get("data");
                    if (b != null) {
                        updateThumbnail(b);

                        if (mBitmap != b) {
                            b.recycle();
                        }
                    }
                    break;
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜