Android调用相机并将照片存储到sd卡上
Android中实现拍照有两种方法,一种是调用系统自带的相机,然后使用其返回的照片数据。 还有一种是自己用Camera类和其他相关类实现相机功能,这种方法定制度比较高,洗染也比较复杂,一般平常的应用只需使用第一种即可。
用Intent启动相机的代码:
new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
拍完照后就可以在onActivityResult(int requestCode, int resultCode, Intent data)中获取到Bitmap对象了。
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
//
检测sd是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
new File("/sdcard/myImage/");
file.mkdirs();
//
创建文件夹
String fileName = "/sdcard/myImage/111.jpg";
try {
b =
new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);
//
把数据写入文件
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
try {
b.flush();
b.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
<
uses-permission
android:name
="android.permission.WRITE_EXTERNAL_STORAGE"
/>
<
uses-permission
android:name
="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
/>
一个demo,实现调用系统相机拍照,将其显示在屏幕上,并且存到sd卡。
完整代码如下:
MyCaremaActivity.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public
class MyCaremaActivity
extends Activity {
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(
new OnClickListener() {
@Override
public
void onClick(View v) {
Intent intent =
new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
});
}
@Override
protected
void onActivityResult(
int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
//
检测sd是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
//
获取相机返回的数据,并转换为Bitmap图片格式
FileOutputStream b =
null;
File file =
new File("/sdcard/myImage/");
file.mkdirs();
//
创建文件夹
String fileName = "/sdcard/myImage/111.jpg";
try {
b =
new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);
//
把数据写入文件
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
try {
b.flush();
b.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);
//
将图片显示在ImageView里
}
}
}
xml version="1.0" encoding="utf-8"
?>
<
LinearLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:orientation
="vertical"
>
<
Button
android:id
="@+id/button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="点击启动相机"
/>
<
ImageView
android:id
="@+id/imageView"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:background
="#999999"
/>
</
LinearLayout
>
xml version="1.0" encoding="utf-8"
?>
<
manifest
xmlns:android
="http://schemas.android.com/apk/res/android"
package
="barry.android.c"
android:versionCode
="1"
android:versionName
="1.0"
>
<
uses-sdk
android:minSdkVersion
="7"
/>
<
uses-permission
android:name
="android.permission.WRITE_EXTERNAL_STORAGE"
/>
<
uses-permission
android:name
="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
/>
<
application
android:icon
="@drawable/ic_launcher"
android:label
="@string/app_name"
>
<
activity
android:label
="@string/app_name"
android:name
=".MyCaremaActivity"
>
<
intent-filter
>
<
action
android:name
="android.intent.action.MAIN"
/>
<
category
android:name
="android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
</
application
>
</
manifest
>
Android调用相机并将照片存储到sd卡上的更多相关文章
- 大过年的,不下班的,上个Android文件操作类(内部存储和sd卡均可)
package com.kkdiangame.UI.res; import java.io.ByteArrayOutputStream; import java.io.File; import jav ...
- Android使用sqlliteOpenhelper更改数据库的存储路径放到SD卡上
假设使用默认的系统管理,默认放在包以下.比較省心.并且在卸载app后不会造成数据残留.可是这样也有一个问题.比方我做一个背单词的软件,那么当用户卸载掉这个app时,他辛辛苦苦下载的单词库也没了... ...
- android调用照相机拍照获取照片并做简单剪裁
引用转载http://www.cnblogs.com/eyu8874521/archive/2012/07/20/2600697.html 效果: 客服端代码: package com.cn.lx ...
- Android数据存储之SD卡
为了更好的存取应用程序的大文件数据,应用程序需要读. 写SD卡上的文件.SD卡大大扩充手机的存储能力. 操作SD首先要加权限: <!--在SDCard中创建与删除文件权限 --> < ...
- File存对象--android 的File存储到SD卡();
方法1:android File存对象--File存储到SD卡(); 1.保存对象到本地或SD卡需要注意的是,要保存的对象(OAuthV1)一定要实现了Serializable接口.实现了Serial ...
- Android 4.0以后正确的获取外部sd卡存储目录
刚解决这个棘手的问题 找了很久,随笔记下. 网上搜索 android 获取外部sd卡存储目录 普遍都是: 1) Environment.getExternalStorageDirectory() 这个 ...
- android学习笔记47——读写SD卡上的文件
读写SD卡上的文件 通过Context的openFileInput.openFileOutput来打开文件输入流.输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限- ...
- android中读取SD卡上的数据
通过Context的openFileInput或者openFileOutput打开的文件输入输出流是操作应用程序的数据文件夹里的文件,这样存储的大小比较有限,为了更好的存取应用程序的大文件数据,应用程 ...
- Android开发之SD卡上文件操作
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
随机推荐
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- redis介绍【转】
Redis新的存储模式diskstore Thursday, Jan 6th, 2011 by Tim | 13 CommentsFiled under: data | Tags: Mongo, Mo ...
- Groovy读取properties及txt
昨晚帮老同事解决了一个SoapUI的代码问题,好长时间没用SoapUI,好多东西都忘了,今天先总结下Groovy读取properties 首先吐槽下SoapUI的apidocs,我几乎从中看不出什么东 ...
- 北邮网关登录python脚本
闲来无聊,来码一发 安装 pip install byrlogin 登录 登出
- DIV背景半透明文字不半透明的样式
DIV背景半透明,DIV中的字不半透明 代码如下:<body bgcolor="#336699"> <div style="filter:alpha(o ...
- c 按范围快速指定整数
以前用过octave, 和matlab类似的软件, 指定范围非常方便 i = 1:10:100; 就可以得到 10 20 30 ... 100 这一系列的数据, 但是在c里面, 必须手动写循环, 太 ...
- 30+学习Web设计和开发的优质新鲜资源
今天我们整理了一些最新的Web设计和开发的资源,这些资源都来自国外的流行站点,不过大家应该不会陌生,放在这里供大家收藏,在需要的时候方便翻阅和学习! 原文地址:http://www.goodfav.c ...
- HDU1243:反恐训练营
题目链接:反恐训练营 题意:本质上是求最大公共子序列,然后加上一个权值 分析:见代码 //公共子序列问题 //dp[i][j]表示前s1的前i个与s的前j个匹配得到的最大公共子序列 #include& ...
- JVM系列二:GC策略&内存申请、对象衰老
JVM里的GC(Garbage Collection)的算法有很多种,如标记清除收集器,压缩收集器,分代收集器等等,详见HotSpot VM GC 的种类 现在比较常用的是分代收集(generatio ...
- VoToucher
VoToucher package com.isoftstone.pcis.policy.common.utils; import com.isoftstone.pcis.policy.common. ...