Dear everyone:

I am facing a problem; when I try to save the picture, the app will
auto stop. but it does not happen everytime.

does anyone know why? Thank you so much.

next is the sourcecode

/**
 * Copyright (c) 2007, Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
 * See the License for the specific language governing permissions
and
 * limitations under the License.
 */

package com.android.cameraapitest;


import java.io.File;

import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;

import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;

import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import android.hardware.Camera;

import android.text.format.DateFormat;
import android.util.Log;

public class CameraApiTest extends Activity implements
SurfaceHolder.Callback
{
    private static final String TAG = "CameraApiTest";
    Camera mCamera;
    boolean mPreviewRunning = false;


    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);

        Log.e(TAG, "onCreate");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
                getWindow().setFlags(WindowManager.LayoutParams.
FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
     //   getWindow().setFormat(PixelFormat.TRANSLUCENT);
                String status=Environment.getExternalStorageState();
                if (status.equals(Environment.MEDIA_MOUNTED)){

        setContentView(R.layout.camera_api_test);
        mSurfaceView = (SurfaceView)findViewById(R.id.surface);

        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
 
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                }
                else{
                        Toast.makeText(getApplicationContext(), "PLease insert 
an SD card
before using the camera!", Toast.LENGTH_LONG).show();
                }

    }

    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        MenuItem item = menu.add(0, 0, 0, "goto gallery");
        item.setOnMenuItemClickListener(new
MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                Uri target = Uri.parse("content://media/external/
images/media");
                Intent intent = new Intent(Intent.ACTION_VIEW,
target);
                startActivity(intent);
                return true;
            }
        });
        return true;
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
    }

    Camera.PictureCallback mPictureCallback = new
Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
                 ContentValues values = new ContentValues(8);
             String newname = DateFormat.format("yyyy-MM-dd kk.mm.ss",
System.currentTimeMillis()).toString();
             values.put(MediaStore.Images.Media.TITLE, newname);
             values.put(MediaStore.Images.Media.DISPLAY_NAME,
newname);
             values.put(MediaStore.Images.Media.DESCRIPTION,
"test");//
             values.put(MediaStore.Images.Media.DATE_TAKEN,
System.currentTimeMillis());//
             values.put(MediaStore.Images.Media.MIME_TYPE, "image/
jpeg");//
             values.put(MediaStore.Images.Media.ORIENTATION, 0);//
             final String CAMERA_IMAGE_BUCKET_NAME = "/sdcard/dcim/
camera";
             final String CAMERA_IMAGE_BUCKET_ID =
String.valueOf(CAMERA_IMAGE_BUCKET_NAME.hashCode());
             File parentFile = new File(CAMERA_IMAGE_BUCKET_NAME);
             String name = parentFile.getName().toLowerCase();
             values.put(Images.ImageColumns.BUCKET_ID,
CAMERA_IMAGE_BUCKET_ID);//id
             values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME,
name);
             Uri uri =
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
             BitmapFactory.Options options=new
BitmapFactory.Options();
                        options.inSampleSize = 2;
                        int quality = 100;
                        Bitmap myImage = BitmapFactory.decodeByteArray(data,
0,data.length,options);
             try {
                 OutputStream outStream =
getContentResolver().openOutputStream(uri);
                 myImage.compress(Bitmap.CompressFormat.JPEG, quality,
outStream);
                 outStream.close();
                 myImage.recycle();
                 return ;
             } catch (Exception e) {
                 Log.e(TAG, "exception while writing image", e);

                }
                }
    };

    public boolean onTrackballEvent (MotionEvent event)
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //return super.onKeyDown(keyCode, event);
                mCamera.takePicture(null, null, mPictureCallback);
                mCamera.stopPreview();
                mPreviewRunning = false;
                mCamera.startPreview();
                mPreviewRunning = true;
            return true;
        }

        return false;
    }

    protected void onResume()
    {
        Log.e(TAG, "onResume");
        super.onResume();
    }

    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
    }

    protected void onStop()
    {
        Log.e(TAG, "onStop");
        super.onStop();
    }

    public void surfaceCreated(SurfaceHolder holder)
    {
        Log.e(TAG, "surfaceCreated");
        mCamera = Camera.open();
        //mCamera.startPreview();
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int
w, int h)
    {
        Log.e(TAG, "surfaceChanged");

        // XXX stopPreview() will crash if preview is not running
        if (mPreviewRunning) {
            mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();
        p.setPreviewSize(w, h);
        mCamera.setParameters(p);
       // mCamera.setPreviewDisplay(holder);
        try {
                        mCamera.setPreviewDisplay(holder);
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        mCamera.startPreview();
        mPreviewRunning = true;
    }

    public void surfaceDestroyed(SurfaceHolder holder)
    {
        Log.e(TAG, "surfaceDestroyed");
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();
    }

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to