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查看文件路径呢?
android自定义camera以及uri和文件路径之间的转换的更多相关文章
- Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...
- Android 自定义View及其在布局文件中的使用示例(二)
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3530213.html From crash_coder linguowu linguowu0622@gami ...
- android 自定义Button,抛弃写shape文件
标签: android 控件 自定义 2017年05月27日 17:52:13 611人阅读 评论(0) 收藏 举报 分类: 自定义View(2) 作者同类文章 X 版权声明:本文为博主原创文章 ...
- C# .Net实现URL绝对路径和相对路径之间互相转换
网站制作开发中,URL的绝对路径和相对路径之间互相转换,是经常需要用到的.以下是在C#.Net下一种实现二者互相转化的方法: [DllImport("shlwapi.dll", C ...
- Android 自定义Camera 随笔
一.权限 <uses-permission android:name="android.permission.CAMERA" /> <uses-permiss ...
- Android与JS进行交互传文件路径
webview+h5这种混合开发最近很火,其中最重要的大概就是java代码和js的交互了,刚接触这东西两天,写写收获. 新建一个assets文件夹,要与res这个文件夹同级,其中存放web项目. 先看 ...
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...
- Android 自定义View及其在布局文件中的使用示例
前言: 尽管Android已经为我们提供了一套丰富的控件,如:Button,ImageView,TextView,EditText等众多控件,但是,有时候在项目开发过程中,还是需要开发者自定义一些需要 ...
- android开发之dip,dp与px像素之间的转换工具,可能用的不多,但是有总比没有好吧。
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985,转载请说明出处. 下面是介绍: 免积分下载地址:http://download.csdn.net/de ...
随机推荐
- noip2015day2-运输计划
题目描述 公元$ 2044 $年,人类进入了宇宙纪元. \(L\) 国有 \(n\) 个星球,还有 \(n-1\) 条双向航道,每条航道建立在两个星球之间,这 \(n-1\) 条航道连通了 \(L\) ...
- 剑指offer-把数组排成最小的数-数组-python
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 思路1:使用 ...
- CentOS 设置 yum源
什么是 yum Yum(全称 Yellow Dog Updater)是一个在 Fedora 和 RedHat 以及 CentOS 中的 Shell 前端软件包管理器.基于 RPM 包管理,能够从指定的 ...
- HTML-复杂动画和变形
1.复杂动画 (1)涉及到的属性: animation-name:动画名称: animation-duration:单次动画总时长: animation-timing-function:时间函数: a ...
- php学习第一天(记录注意事项)
- MySQL的简单条件判断语句
在MySQL中条件判断语句常用于数据转换,基于现有数据创建新的数据列,使用场景还是比较多. 基础样式: CASE WHEN`条件`THEN`结果` ELSE`默认结果` END 在同一条判断语句中可以 ...
- Azure云服务托管恶意软件
微软Azure云服务被用于托管恶意软件,可控制多达90台电脑 BleepingComputer称,在早期报道中,5月份陆续出现了两起与Azure相关的恶意软件攻击事件: 1.自5月10日以来,Azur ...
- scp 从另一台linux服务器拷贝文件或文件目录
格式:scp [参数] [原路径] [目标路径] download 使用方法:scp -r root@127.0.0.1:/opt/soft/test /opt/soft/ scp -r 用户名@IP ...
- BZOJ[3252]攻略(长链剖分)
BZOJ[3252]攻略 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏<XX半岛> ...
- Spring mvc项目的web.xml以及注释
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp ...