W
W
wolfak2016-02-02 18:54:32
Java
wolfak, 2016-02-02 18:54:32

How to get full photo from camera in android?

Good evening. All day I struggle with capturing a photo from the camera. Except how to get a miniature copy of the photo, nothing comes of it.
I follow these instructions:
developer.alexanderklimov.ru/android/photocamera.php
A file with an image from the camera camera is successfully created in this way:

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

 imgCamFile = new File(Environment.getExternalStorageDirectory(), "tempcamera.jpg");
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgCamFile));

mCurrentPhotoPath = imgCamFile.getAbsolutePath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
thumbnailBitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/tempcamera.jpg", options);

startActivityForResult(takePicture, 0);

I'm trying to extract the bitmap from the file into the thumbnailBitmap variable, but nothing happens. Either an access error, or no error at all, but the thumbnailBitmap remains null. How to get a bitmap? The file is created in the root directory successfully.
Replacing the contents of decodeFile with mCurrentPhotoPath , or Uri.fromFile(imgCamFile).toString() still doesn't help.
Manifesto:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"></uses-feature>

Hope for your help, thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Dorofeev, 2016-02-02
@wolfak

To get the image in full size, you need to first
force the camera to save the image in a temporary file:

private URI mImageUri;

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo;
    try
    {
        // место где хранятся фото
        photo = this.createTemporaryFile("picture", ".jpg");
        photo.delete();
    }
    catch(Exception e)
    {
        Log.v(TAG, "Не получилось создать фото!");
        Toast.makeText(activity, "Пожалуйста, проверьте Sd  - карту", 10000);
        return false;
    }
    mImageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    //вызываем Intent камеры
    activity.startActivityForResult(this, intent, MenuShootImage);

private File createTemporaryFile(String part, String ext) throws Exception
{
    File tempDir= Environment.getExternalStorageDirectory();
    tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");  
    if(!tempDir.exists())
    {
        tempDir.mkdir();
    }
    return File.createTempFile(part, ext, tempDir);
}

Then after writing the image - just pull the image from imageUri:
public void grabImage(ImageView imageView)
{
    this.getContentResolver().notifyChange(mImageUri, null);
    ContentResolver cr = this.getContentResolver();
    Bitmap bitmap;
    try
    {
        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
        imageView.setImageBitmap(bitmap);
    }
    catch (Exception e)
    {
        Toast.makeText(this, "Ошибка загрузки", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "Ошибка загрузки", e);
    }
}


//вызов камеры после выполнения
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    if(requestCode==MenuShootImage && resultCode==RESULT_OK)
    {
       ImageView imageView;
       //делаем, что нужно с картинкой
       this.grabImage(imageView);
    }
    super.onActivityResult(requestCode, resultCode, intent);
}

1) Make sure the permissions are in the right place:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
    ...

</application>

2) Do you happen to have Marshmallow(23 API)?
If so, then the permissions should also be checked in the code:
/ Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

Update 2 :
Compatibility with previous versions:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M){

} else{
    
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question