RedApparat / Fotoapparat

Making Camera for Android more friendly. 📸
Apache License 2.0
3.82k stars 408 forks source link

PhotoResult.toBitmap().whenAvailable(Callback<BitmapPhoto>) does nothing #128

Closed vic797 closed 7 years ago

vic797 commented 7 years ago

With an Alcatel A3 XL.

I am having some problems while taking photos. I want to take a photo and return it as an intent extra but:

Note that I don't want to save the picture to a file in this activity.

This is my activity. I don't know if I am doing something wrong.

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import io.fotoapparat.Fotoapparat;
import io.fotoapparat.parameter.LensPosition;
import io.fotoapparat.photo.BitmapPhoto;
import io.fotoapparat.result.PendingResult;
import io.fotoapparat.result.PhotoResult;
import io.fotoapparat.view.CameraView;

import static io.fotoapparat.parameter.selector.AspectRatioSelectors.standardRatio;
import static io.fotoapparat.parameter.selector.FlashSelectors.autoFlash;
import static io.fotoapparat.parameter.selector.FlashSelectors.autoRedEye;
import static io.fotoapparat.parameter.selector.FlashSelectors.off;
import static io.fotoapparat.parameter.selector.FlashSelectors.torch;
import static io.fotoapparat.parameter.selector.FocusModeSelectors.autoFocus;
import static io.fotoapparat.parameter.selector.FocusModeSelectors.continuousFocus;
import static io.fotoapparat.parameter.selector.FocusModeSelectors.fixed;
import static io.fotoapparat.parameter.selector.LensPositionSelectors.back;
import static io.fotoapparat.parameter.selector.LensPositionSelectors.lensPosition;
import static io.fotoapparat.parameter.selector.Selectors.firstAvailable;
import static io.fotoapparat.parameter.selector.SensorSensitivitySelectors.highestSensorSensitivity;
import static io.fotoapparat.parameter.selector.SizeSelectors.biggestSize;

public class CameraActivity extends AppCompatActivity {

    private CameraView cameraView;
    private Fotoapparat fotoapparat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        findViewById(R.id.take_photo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoResult result = fotoapparat.takePicture();
                result.toBitmap().whenAvailable(new PendingResult.Callback<BitmapPhoto>() {
                    @Override
                    public void onResult(BitmapPhoto bitmapPhoto) {
                        Intent intent = new Intent();
                        intent.putExtra("data", bitmapPhoto.bitmap);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });
            }
        });
        cameraView = (CameraView) findViewById(R.id.camera);
        fotoapparat = Fotoapparat.with(this)
                .into(cameraView)
                .lensPosition(back())
                .photoSize(standardRatio(biggestSize()))
                .lensPosition(lensPosition(LensPosition.BACK))
                .focusMode(firstAvailable(
                        continuousFocus(),
                        autoFocus(),
                        fixed()
                ))
                .flash(firstAvailable(
                        autoRedEye(),
                        autoFlash(),
                        torch(),
                        off()
                ))
                .sensorSensitivity(highestSensorSensitivity())
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        fotoapparat.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        fotoapparat.stop();
    }
}
dmitry-zaitsev commented 7 years ago

Please do not put Bitmaps into Intent - there is a limitation on how much data you can fit in there and Bitmap is certainly going over that limit.

Consider saving image to a file and then returning just a file path.