Android Camera Issues

Hope someone can give some pointers (or the correct answer) ... A simple application, take a picture using the built-in camera application, save the image in a separate application. Let's do it.

Problem. The camera application saves the image in the default location of the application (/ mnt / sdcard / external_sd / DCIM / Camera), as well as my user path (in the code below). Both files are exactly the same except for the file name. The external_sd file (the one I want to delete) is saved with a dash (-) compared to my own file path saved with underscores. The file size is exactly the same.

How to stop this double image problem? Is there an additional option of intention that I am missing? Or am I doing this completely wrong, is something missing? I am using 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;
    }
}
+3
source share
4 answers

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

+1
source

Delete 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 the intent:

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

Therefore, the image will not be saved on the SD card or by default.

0
source

. , , , , , MediaStore ( : )

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

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

I had this problem, 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 doesn’t quite solve, but it works for me;)

0
source

Source: https://habr.com/ru/post/1786050/


All Articles