0

I wanted to take a photo and save it to gallery. Then read this photo from gallery and convert it to text( using ocr)

But my app keep crashing when I try to save it with this line of code

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

I debug my app and found this line is the responsible for crash. But without this line I can't save my image. How can I do that ?

package com.example.takepicture;

import ...

public class MainActivity extends AppCompatActivity {   
    private static final int CAMERA_REQUEST = 1888;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        Button btnCamera = (Button) findViewById(R.id.btnCamera);
        imageView = (ImageView) findViewById(R.id.ImageView); 

        btnCamera.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                String pictureName = getPictureName();

                File imageFile = new File(pictureDirectory,pictureName);
                Uri pictureUri = Uri.fromFile(imageFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
                //capturarFoto();


            }
        });

    }

    private String getPictureName() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        return "Img" + timeStamp + ".jpg";


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

        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_OK) {
            if (requestCode == CAMERA_REQUEST) {

            }
        }
    }
}
Dr Mido
  • 2,414
  • 4
  • 32
  • 72
muratcanyeldan
  • 111
  • 2
  • 14

1 Answers1

0

Fastest solution is adding:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

somewhere before calling: startActivityForResult(...)

But the correct one (for API >24) is:

  • creating URI via FileProvider.getUriForFile(...) and

  • creating provider (and adding it to Manifest file)

You can find more info there: https://stackoverflow.com/a/50265329/5529263

Boken
  • 4,825
  • 10
  • 32
  • 42