How to Capture an Image and Store It with the Native Android Camera

How to capture an image and store it with the native Android Camera

Have you checked what the output of Environment.getExternalStorageDirectory() is, because if it does not contain a trailing file seperator (/) then your image will end up in a directory that does not reside on the SDcard such as:

 /mnt/sdcardmake_machine_example.jpg

When what you really want is:

 /mnt/sdcard/make_machine_example.jpg

Try this code instead:

 _path = Environment.getExternalStorageDirectory() + File.separator +  "make_machine_example.jpg";

How can I capture a new image using the camera and after that store it in the external storage?

Your file will not be scanned by the MediaStore and hence will not be visible in Gallery apps as Gallery apps mostly get their info from MediaStore.

getExternalFilesDir() is a location private for your app and the MediaStore respects that.

How can I capture a new image using the camera and after that store it in the external storage?

Wrong problem description.

Once the camera app took the picture the camera app will save the picture to the file indicated by the file provider. There is nothing to do more then.

So before starting the camera and even before using FileProvider you should have determined a suitable location for your file and builded a nice uri for that file.

You have at least two options.

Use MediaStore to get an uri for a file in public DCIM or Pictures directory.

Use getExternalStoragePublicDirectory() with FileProvider to get an uri for a file in the same public directories.

Capture Image from Camera and Display in Activity

Here's an example activity that will launch the camera app and then retrieve the image and display it.

package edu.gvsu.cis.masl.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity
{
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private static final int MY_CAMERA_PERMISSION_CODE = 100;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
}
else
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
});
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_PERMISSION_CODE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
else
{
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}

Note that the camera app itself gives you the ability to review/retake the image, and once an image is accepted, the activity displays it.

Here is the layout that the above activity uses. It is simply a LinearLayout containing a Button with id button1 and an ImageView with id imageview1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button>
<ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>

</LinearLayout>

And one final detail, be sure to add:

<uses-feature android:name="android.hardware.camera"></uses-feature> 

and if camera is optional to your app functionality. make sure to set require to false in the permission. like this

<uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>

to your manifest.xml.

Android where can I get image taken by native camera app

To capture images using camera call this intent

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, requestCode);

and to handle the callback use onActivityResult function

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

Bitmap mImageBitmap;
Bundle extras = data.getExtras();
mImageBitmap = Bitmap.createScaledBitmap(
(Bitmap) extras.get("data"), 100, 100, false);
}

mImageBitmap will hold the image that you captured. Hope it helps :)

Launch native camera app with option to capture either video or image

Currently As specified by you there are 2 intents for capturing image and video i.e. MediaStore.ACTION_IMAGE_CAPTURE & MediaStore.ACTION_VIDEO_CAPTURElink.

For intially I was also thinking startActivityForResult (Intent intent, int requestCode, Bundle options), Here options param was used to give the extras for the external applications but we can only specify some animations not the values as specified link1 & link2.

Solution

I would rather suggest you to go for the custom app with options as you like rather than the restrictions given by the existing camera app.

How to take a picture and get the data in React Native with the mobile camera

The simplest way to do this would be to use the ImagePicker library https://github.com/react-community/react-native-image-picker

This allows you to open the native camera and get the provides a callback with the data from the photo.

ImagePicker.launchCamera(options, (response)  => {
// Response data
});

Android’s New Image Capture from a Camera using File Provider and saving in root directory

As suggested in blackapps comment, using

File imageFile = new File(getExternalFilesDir(Environment.DIRECTORY_PUCTURES), fileName); 

instead of

imageFile = File.createTempFile(imageName, ".jpg", storageDir);

solves the problem.

Using android's native Camera App to capture photos throws SecurityException

The issue is the revoked permission ("with revoked permission android.permission.CAMERA"). Normally, you do not need permission, but if you ask for permission (say, in a previous build of your app), and the user revokes that permission, then you cannot use ACTION_IMAGE_CAPTURE.



Related Topics



Leave a reply



Submit