开发者

Android Camera Intent woes

开发者 https://www.devze.com 2023-02-05 09:08 出处:网络
Hope someone may give some pointers (or an out right answer)... Simple app, take an image using the built-in camera app, save the image to a separate application.Be done.

Hope someone may give some pointers (or an out right answer)... Simple app, take an image using the built-in camera app, save the image to a separate application. Be done.

Problem: The camera application saves the image in the default app location (/mnt/sdcard/external_sd/DCIM/Camera) as well as my custom path (in code below). Both files are exactly the same except for the file name. The external_sd file (the one I want gone) is saved with dashes (-) vs my custom file path saved with underscores. File sizes are exactly the same.

How can I stop this double image issue? Is there an extra intent option I'm missing? Or am I doing this completely wrong, missing something? I'm using a Galaxy S Vibrant.

Code snippet:

private static Uri _outputFileUri;
private static File _file;
private ImageView _image;
private SimpleDateFormat _simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); 


    _takePicture = (Button) findViewById(R.id.takePicture);
    _takePicture.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            _intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);          

  开发者_如何学编程          _file = new File(Environment.getExternalStorageDirectory() +
                            "/Android/data/my own folder/files/",
                             _simpleDateFormat.format(new Date()).toString() + 
                             ".jpg");

            _outputFileUri = Uri.fromFile(_file);

            _intent.putExtra(MediaStore.EXTRA_OUTPUT, _outputFileUri);
            startActivityForResult(_intent, CAMERA_ACTIVITY);
        }
    });   


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

    if (resultCode == RESULT_CANCELED) {
        Toast.makeText(this, "Activity cancelled", Toast.LENGTH_LONG).show();
        return;
    }

    switch (requestCode) {  

        case CAMERA_ACTIVITY: 

            if (resultCode == RESULT_OK) {

                try{
                    Bitmap b = MediaStore.Images.Media.getBitmap(getContentResolver(), _outputFileUri);
                    _image.setImageBitmap(b);
                    _image.invalidate();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
            break;
    }
}


This is device-dependent behavior. My observation is that HTC devices do not have this duplication problem, but Samsung devices do.


Please remove the following lines:

_file = new File(Environment.getExternalStorageDirectory() +
                            "/Android/data/my own folder/files/",
                             _simpleDateFormat.format(new Date()).toString() + 
                             ".jpg");

            _outputFileUri = Uri.fromFile(_file);

            _intent.putExtra(MediaStore.EXTRA_OUTPUT, _outputFileUri);

Also update the code to get the image from intent:

Bitmap b = (Bitmap) data.getExtras().get("data");
_image.setImageBitmap(b);
_image.invalidate();

This way picture wouldn't be saved on sd card or default location.


I had the same problem and gave up. Sometime later I found out that I was not getting it anymore and I'm not sure what change I made to my code, but I think that it was MediaStore's fault (check my unsolved question: Weird camera Intent behavior)

As you already have the image URI, why don't you use it to set the ImageViews' bitmap?

// void setImageURI(Uri uri)
 _image.setImageBitmap(_outputFileUri);


I had this issue and here is how i solved it :

File createImageFile() throws IOException{

    String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String filename = "IMG_"+timestamp+"_";
    File image = File.createTempFile(filename,".jpg",mGalleryFolder );
    if (image.length() == 0 ){
        boolean delete = image.delete();
    }
    mLocation = image.getAbsolutePath();
    return image;
}

It's not exactly solving but works for me ;)

0

精彩评论

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