android自定义camera以及uri和文件路径之间的转换

相对直接调用系统的camera,这种方法使用得相对还少一些。根据api文档,步骤如下:

定义一个预览类

可以参照《android高薪之路》这本书上面,有这种方法的一种完整实现

而对应的activity如下:

package com.bobo.mycamara;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class MyCameraActivity extends Activity {
    private Camera mCamera;
    private Button btn_capture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.mycamera_layout);
        btn_capture = (Button) this.findViewById(R.id.button_capture);
        mCamera = getCameraInstance();
        MyCameraPreview mPreview = new MyCameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
        btn_capture.setOnClickListener(new CaptureListener());
    }

    class CaptureListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // 这里进行拍照操作
            mCamera.takePicture(null, null, mPicture);
        }

    }

    // 拍照结束之后的回调接口
    private PictureCallback mPicture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // 将文件进行存储
            System.out.println(new String(data));
            File mediaFile = getMediaFile();
            try {
                FileOutputStream fos = new FileOutputStream(mediaFile);
                // 这样保存出来的也是图像格式
                fos.write(data);
                // 不过如果使用下面的方法,可以在保存之前利用bitmap的相关方法,对相应的bitmap进行进一步的调整,如借助matrix
                // Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0,
                // data.length);
                // bitmap.compress(CompressFormat.JPEG, 100, fos);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    };

    // 当activity暂停的时候一定要记得将camera资源release掉
    @Override
    protected void onPause() {
        super.onPause();
        if (this.mCamera != null) {
            this.mCamera.release();
            this.mCamera = null;
        }

    }

    protected Uri getFileUri(File file) {
        return Uri.fromFile(file);
    }

    protected File getMediaFile() {
        File mediaDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "myCamera");
        if (!mediaDir.exists()) {
            if (!mediaDir.mkdirs()) {
                return null;
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());
        File mediaFile;
        mediaFile = new File(mediaDir.getPath() + File.separator + "IMG_"
                + timeStamp + ".jpg");

        return mediaFile;

    }

    // 获得系统的相机
    public Camera getCameraInstance() {
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        } catch (Exception e) {
        }
        return c;
    }

}

 

private String getRealPath(Uri fileUrl) { 
        String fileName = null; 
        Uri filePathUri = fileUrl; 
        if (fileUrl != null) { 
            if (fileUrl.getScheme().toString().compareTo("content") == 0) { 
                // content://开头的uri 
                Cursor cursor = getContentResolver().query(fileUrl, null, null, 
                        null, null); 
                if (cursor != null && cursor.moveToFirst()) { 
                    int column_index = cursor.getColumnIndexOrThrow("_data"); 
                    fileName = cursor.getString(column_index); // 取出文件路径 
                    if (!fileName.startsWith("/mnt")) { 
                        // 检查是否有”/mnt“前缀 
  
                        fileName = "/mnt" + fileName; 
                    } 
                    cursor.close(); 
                } 
            } else if (fileUrl.getScheme().compareTo("file") == 0) { 
                // file:///开头的uri 
                fileName = filePathUri.toString(); 
                fileName = filePathUri.toString().replace("file://", ""); 
                // 替换file:// 
                if (!fileName.startsWith("/mnt")) { 
                    // 加上"/mnt"头 
                    fileName += "/mnt"; 
                } 
            } 
        } 
        return fileName; 
    }

同时别忘了增添了相应的权限。这里有将文件路径转换为uri的方法,反过来如何通过uri查看文件路径呢?

 

posted @ 2013-06-20 16:08  bobo的学习笔记  阅读(1318)  评论(0编辑  收藏  举报