1

I'm fairly new to android development and I'm trying to create an app that calls implicit intent for the camera by clicking one button. Once the image is captured you can click back button to get to main activity. In the main activity there's second button that when you click it you can see recent files and the captured image should be showing there

I was working through https://developer.android.com/training/camera/photobasics.html#TaskScalePhoto

Used the following code for capturing the image

final TextView textviewcamera = (TextView) findViewById(R.id.TextView1); final int REQUEST_IMAGE_CAPTURE = 1;

    // Set an OnClickListener on this Text View
    // Called each time the user clicks the Text View
    textviewcamera.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
        /*
            opens the camera app and we are able to take a photo, photo is not save anywhere, needs to be fixed
            code is from android studio, DON'T FORGET to cite
            https://developer.android.com/training/camera/photobasics.html

         */
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //if (takePictureIntent.resolveActivity(getPackageManager()) != null)
            //{
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            //}

        }

Then I have another piece of code that shows the recent files

final TextView textviewpicture = (TextView) findViewById(R.id.TextView2);

    // Set an OnClickListener on this Text View
    // Called each time the user clicks the Text View
    textviewpicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        /*
            opens the camera app and we are able to take a photo, photo is not save anywhere, needs to be fixed
            code is from android studio, DON'T FORGET to cite
            https://developer.android.com/training/camera/photobasics.html

         */
            Intent viewpicture = new Intent();
            viewpicture.setType("image/*");
            viewpicture.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(viewpicture, "Select Picture"), 10);
        }
    });

I'm able to open the camera and take the photo however when I try to view it in my recent files, this part is not working.

Any help would be highly appreciated

Thanks a mill everyone :)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ingells
  • 11
  • 3
  • `ACTION_IMAGE_CAPTURE` will deliver the image to your app, in the `onActivityResult()` call. Since you did not put `EXTRA_OUTPUT` on the `ACTION_IMAGE_CAPTURE` `Intent`, you will get a thumbnail `Bitmap`. This image does not have to be saved to the device — in fact, if it does, that's arguably a bug in the camera app. Hence, since the camera app is not saving the image, and since you are not saving the image, no other app will be able to see the image. – CommonsWare Oct 29 '17 at 18:24
  • Thank you very much for getting back to me. What can I add to EXTRA_OUTPUT, so the image will save in the gallery. I tried to add intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri); I'm just getting an error for captureImageUri...what can I enter there, so the image will be saved into the gallery? – Ingells Oct 29 '17 at 20:46
  • I do not know what the error is, so I cannot provide you with much advice on that. [Here is a sample app](https://github.com/commonsguy/cw-omnibus/tree/v8.7/Camera/FileProvider) that demonstrates the use of `EXTRA_OUTPUT`, having the camera app save the image to a location managed by a `FileProvider`. – CommonsWare Oct 29 '17 at 21:20

1 Answers1

0

override your onActivityresult there you can preview your image from there save it to external storage

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
case REQUEST_IMAGE_CAPTURE:
            if (resultCode != RESULT_CANCELED)
            {
            if (resultCode == RESULT_OK) {

                BitmapFactory.Options options = new 
                BitmapFactory.Options();
                options.inSampleSize = 8;
        Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                options);
           saveImageToExternalStorage(bitmap)
            }
    } else{
     //show error message 
    } 
  } }

public static File saveImageToExternalStorage(Bitmap finalBitmap) {
    File file;
    String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File myDir = new File(root + "/easyGovs");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".png";
    file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.PNG, 55, out);
        out.flush();
        out.close();
        return file;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return file;
}

and while passing your intent for camera use this -

     private Uri fileUri;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
Apoorv Singh
  • 1,295
  • 1
  • 14
  • 26