0

I am trying to create an android application that will take an image and display it to user for further actions.

This is the Camera activity. It is started when the user presses a floating action button from the previous activity. The Camera intent is triggered within onCreate itself.

Camera action is working perfectly, it takes photo just fine. But neither it is getting saved in the gallery nor it is getting displayed in the ImageView defined below.

I am trying to display the captured image inside the ImageView but the camera activity just moves back to the menu activity without displaying anything. How can I solve this problem ?

public class Camera extends AppCompatActivity {

public static final String TAG = Camera.class.getSimpleName();
static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView = (ImageView)findViewById(R.id.mImageView);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_layout);



    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (cameraIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.i(TAG, "IOException");
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  // prefix
            ".jpg",         // suffix
            storageDir      // directory
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

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

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        try {
            Log.v("This is totally working", "Yeah!");
            mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
            mImageView.setImageBitmap(mImageBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

This is the XML code for the layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
    android:id="@+id/mImageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="7" />


</FrameLayout>
Anand MP
  • 121
  • 1
  • 2
  • 13
  • The coding is working now. I made slight modifications to the previous activity from which this activity was initialized. Please make sure that mImageView = (ImageView)findViewById(R.id.mImageView) is intialized only after you have the created setContentView() otherwise it would result in a View not found null error. – Anand MP Jan 31 '16 at 07:01

2 Answers2

0

Add WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml

0x5050
  • 1,221
  • 1
  • 17
  • 32
0

You can try with Magical Take Photo library.

1. compile in gradle

 compile 'com.frosquivel:magicaltakephoto:1.0'

2. You need this permission in your manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA"/>

3. instance the class like this

// "this" is the current activity param

MagicalTakePhoto magicalTakePhoto =  new MagicalTakePhoto(this,ANY_INTEGER_0_TO_4000_FOR_QUALITY);

4. if you need to take picture use the method

magicalTakePhoto.takePhoto("my_photo_name");

5. if you need to select picture in device, try with the method:

 magicalTakePhoto.selectedPicture("my_header_name");

6. You need to override the method onActivityResult of the activity or fragment like this:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        magicalTakePhoto.resultPhoto(requestCode, resultCode, data);
        //example to get photo
        //imageView.setImageBitmap(magicalTakePhoto.getMyPhoto());
    }

Note: Only with this Library you can take and select picture in the device, this use a min API 15.

fabian7593
  • 77
  • 1
  • 2