-1

I am new to programming and especially Android. I am making a simple application where I am invoking the camera and taking a picture, the picture is then displayed in an ImageView but I also want it to be stored in the gallery when I click the button to view the stored pictures.

My Code:


package com.example.picture.app;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    public static final int CAMERA_REQUEST = 10;
    private ImageView imgView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //get access to the image view

        imgView = findViewById(R.id.imgView);
    }
    //this method will be called when the take photo button is clicked
    public void btnTakePhotoClicked(View v) {

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);

    }

    public void onImageGalleryClicked(View v) {

        //invoke the image gallery using n implicit intent

        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);

        //where do we want to find the data

        File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

        String pictureDirectoryPath = pictureDirectory.getPath();

        //get a uri representation

        Uri data = Uri.parse(pictureDirectoryPath);

        //set the data and the type. Get all image types


        photoPickerIntent.setDataAndType(data, "image/*");

        startActivityForResult(photoPickerIntent, 20);

    }

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

        //DID THE USER CHOOSE OK? If so code inside this code will execute
        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA_REQUEST) {

                //WE ARE HEARING BACK FROM THE CAMERA

                Bitmap cameraImage = (Bitmap) data.getExtras().get("data");
                //at this point we have the image from the camera

                imgView.setImageBitmap(cameraImage);

            }

        }

    }
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
Simon27
  • 1
  • 1
  • 2
  • You need to write the image on phone memory after showing it in imageview . – Faisal Jan 22 '18 at 18:03
  • Please note that the bitmap you receive from the CAMERA_REQUEST is very small, it's a thumbnail that should not be saved on disk, it can be used as an icon on your screen. – Alex Cohn Jan 22 '18 at 18:50

1 Answers1

1

You can use the compress() method to save it in External storage.

You can try this


try
    { 
        FileOutputStream out = new FileOutputStream(new File(Environment.getExternalStorageDirectory().toString() + "/bmp.png")); 
        cameraImage.compress(Bitmap.CompressFormat.PNG, 100, out); 
        out.flush(); 
        out.close(); 
    } 
    catch (Exception e)
    {
        Toast.makeText(getBaseContext(),e.getMessage(), Toast.LENGTH_SHORT);
    }

Also, you have to add uses permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

AA Shakil
  • 538
  • 4
  • 14