Search code examples
androidandroid-cameraimagepreview

Preview Picture on my Android Aplication


I have an application where I take pictures from my app.

It is working fine on Motorola RAZR HD Android 4.1.2 and a Galaxy S3 Android 4.1.1.

The only difference between the two phones is when I take pictures using my app with Motorola RAZR I can preview the image and when I use with Samsung Galaxy S3 this is not working.

I want to preview the image in Samsung too. How I can do that?

This code it's on my Principal Activity

setContentView(R.layout.principal_activity);
principalActivity_bt_TirarFoto = (Button) findViewById(R.id.principalActivity_bt_TirarFoto);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
cameraSurfaceView = new CameraSurfaceView(principalActivity_bt_TirarFoto.getContext());
cameraSurfaceView.setSoundEffectsEnabled(true);
cameraSurfaceView.setDrawingCacheEnabled(true);
preview = (FrameLayout) findViewById(R.id.principalActivity_fl_Camera);

preview.addView(cameraSurfaceView);

principalActivity_bt_TirarFoto.setSoundEffectsEnabled(true);
principalActivity_bt_TirarFoto.setOnClickListener(new OnClickListener() {

@SuppressLint("NewApi") 
@Override
public void onClick(View v) {
    Log.i("PrincipalActivity","onClick - TirarFoto");
camera = cameraSurfaceView.getCamera();
camera.takePicture(new ShutterCallback() {

@Override
    public void onShutter() {
        AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mgr.playSoundEffect(AudioManager.FLAG_PLAY_SOUND);

                }
            }, null, hps = new HandlePictureStorage());

            imageSelected = false;

            mostrarBotoesConfirma();

        }
    });

And this is my PictureCallback

public class HandlePictureStorage implements PictureCallback {

int wid = 0;
Bitmap newImage;

public HandlePictureStorage(){
}

@Override
public void onPictureTaken(byte[] picture, Camera camera) {

    Matrix matrix = new Matrix();

    Bitmap cameraBitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length);

    wid = 612;

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(cameraBitmap, wid, wid, true);
    Bitmap imagemRotacionada = Bitmap.createBitmap(scaledBitmap,0,0,scaledBitmap.getWidth(),scaledBitmap.getHeight(),matrix,true);

    newImage = Bitmap.createBitmap (612, 612, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(newImage);
    canvas.drawBitmap(imagemRotacionada, 0f, 0f, null);

    cameraBitmap.recycle();
    cameraBitmap = null;
}

public Bitmap getBitmap(){
    return newImage;
}

}

Thanks!

== EDIT 1 ==

Here's the logcat @Segi. I run my app and take a picture and this is the log he presented.

06-10 08:53:50.965: I/PrincipalActivity(14549): onCreate
06-10 08:53:50.985: D/dalvikvm(14549): GC_FOR_ALLOC freed 3074K, 19% free 17673K/21575K, paused 9ms, total 9ms
06-10 08:53:51.015: D/dalvikvm(14549): GC_FOR_ALLOC freed <1K, 12% free 19135K/21575K, paused 8ms, total 8ms
06-10 08:53:51.020: I/dalvikvm-heap(14549): Grow heap (frag case) to 21.763MB for 2663440-byte allocation
06-10 08:53:51.040: D/dalvikvm(14549): GC_CONCURRENT freed 0K, 11% free 21737K/24199K, paused 11ms+2ms, total 21ms
06-10 08:53:51.040: D/dalvikvm(14549): WAIT_FOR_CONCURRENT_GC blocked 2ms
06-10 08:53:51.095: D/dalvikvm(14549): GC_CONCURRENT freed 2160K, 12% free 21580K/24327K, paused 1ms+2ms, total 10ms
06-10 08:53:51.095: D/dalvikvm(14549): WAIT_FOR_CONCURRENT_GC blocked 7ms
06-10 08:53:51.115: I/Teste(14549): last picture (20130610_025810) taken on: 2013-06-10
06-10 08:53:51.115: I/System.out(14549): Not a DRM File, opening notmally
06-10 08:53:51.120: I/System.out(14549): buffer returned 
06-10 08:53:51.130: D/dalvikvm(14549): GC_FOR_ALLOC freed 230K, 10% free 21968K/24327K, paused 8ms, total 9ms
06-10 08:53:51.160: I/dalvikvm-heap(14549): Grow heap (frag case) to 52.470MB for 31961104-byte allocation
06-10 08:53:51.180: D/dalvikvm(14549): GC_CONCURRENT freed <1K, 5% free 53180K/55559K, paused 11ms+2ms, total 21ms
06-10 08:53:51.180: D/dalvikvm(14549): WAIT_FOR_CONCURRENT_GC blocked 10ms
06-10 08:53:51.180: W/CursorWrapperInner(14549): Cursor finalized without prior close()
06-10 08:53:51.180: D/dalvikvm(14549): WAIT_FOR_CONCURRENT_GC blocked 10ms
06-10 08:53:51.535: I/Entrou no Resume(14549): Entrou no Resume
06-10 08:53:51.590: I/CameraSurfaceView(14549): Surface Created
06-10 08:53:51.910: I/Camera Width: 320(14549): Camera Height: 240
06-10 08:53:51.940: I/CameraSurfaceView(14549): Surface Changed
06-10 08:53:52.415: E/SpannableStringBuilder(14549): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-10 08:53:52.415: E/SpannableStringBuilder(14549): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-10 08:54:01.935: I/PrincipalActivity(14549): onClick - TirarFoto
06-10 08:54:02.150: D/dalvikvm(14549): GC_FOR_ALLOC freed 110K, 5% free 53261K/55559K, paused 12ms, total 12ms
06-10 08:54:02.155: I/dalvikvm-heap(14549): Grow heap (frag case) to 55.186MB for 2764816-byte allocation
06-10 08:54:02.185: D/dalvikvm(14549): GC_CONCURRENT freed 5067K, 13% free 50893K/58311K, paused 12ms+2ms, total 29ms
06-10 08:54:02.215: D/dalvikvm(14549): GC_FOR_ALLOC freed <1K, 11% free 52357K/58311K, paused 13ms, total 13ms
06-10 08:54:02.230: D/dalvikvm(14549): GC_FOR_ALLOC freed <1K, 8% free 53820K/58311K, paused 13ms, total 13ms

Thanks again!!!


Solution

  • Ok.

    I found a solution for this.

    Like @Segi said before. S3 has a bug and to avoid this I did.

    First I get the model of the phone

    String phoneModel = android.os.Build.MODEL;
    

    Then I verify when I takePicture the model. If it's a Galaxy S3 I use a method setBackground of my cameraSurfaceView. Here is the code.

    if (phoneModel.equals("GT-I9305T")){
       camera.takePicture(new ShutterCallback() {
    
       @Override
       public void onShutter() {
           AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
           mgr.playSoundEffect(AudioManager.FLAG_PLAY_SOUND);
       }
    }, null, new Camera.PictureCallback(){
    
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
    
        int wid = 0;
    
        Matrix matrix = new Matrix();
        matrix.setRotate(90);
    
        Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    
        wid = 612;
    
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(cameraBitmap, wid, wid, true);
        Bitmap imagemRotacionada = Bitmap.createBitmap(scaledBitmap,0,0,scaledBitmap.getWidth(),scaledBitmap.getHeight(),matrix,true);
    
        imagemS3 = Bitmap.createBitmap (612, 612, Bitmap.Config.ARGB_8888);
    
        Canvas canvas = new Canvas(imagemS3);
        canvas.drawBitmap(imagemRotacionada, 0f, 0f, null);
    
        cameraBitmap.recycle();
        cameraBitmap = null;
    
        scaledBitmap.recycle();
    
        imagemRotacionada.recycle();
    
        Drawable imageDrawable = new BitmapDrawable(getResources(),imagemS3);
        cameraSurfaceView.setBackground(imageDrawable);
    
    }});
    

    or you can see where I found the solution here.

    S3(GT-I9300) Camera preview holder bug

    Thanks!