Android調(diào)用相機(jī)并將照片存儲(chǔ)到sd卡上實(shí)現(xiàn)方法
更新時(shí)間:2012年12月05日 15:16:55 作者:
Android中實(shí)現(xiàn)拍照有兩種方法,一種是調(diào)用系統(tǒng)自帶的相機(jī),還有一種是自己用Camera類和其他相關(guān)類實(shí)現(xiàn)相機(jī)功能,這種方法定制度比較高,需要的朋友可以了解下
Android中實(shí)現(xiàn)拍照有兩種方法,一種是調(diào)用系統(tǒng)自帶的相機(jī),然后使用其返回的照片數(shù)據(jù)。 還有一種是自己用Camera類和其他相關(guān)類實(shí)現(xiàn)相機(jī)功能,這種方法定制度比較高,洗染也比較復(fù)雜,一般平常的應(yīng)用只需使用第一種即可。
用Intent啟動(dòng)相機(jī)的代碼:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);拍完照后就可以在onActivityResult(int requestCode, int resultCode, Intent data)中獲取到Bitmap對(duì)象了。Bitmap bitmap = (Bitmap) data.getExtras().get("data");
要將圖像存儲(chǔ)到sd卡之前最好先檢查一下sd卡是否可用
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測(cè)sd是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
以下代碼可以實(shí)現(xiàn)將圖像文件存到“sdcard/myImage/”文件夾下,名稱為“111.jpg”
File file = new File("/sdcard/myImage/");
file.mkdirs();// 創(chuàng)建文件夾
String fileName = "/sdcard/myImage/111.jpg";
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數(shù)據(jù)寫(xiě)入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
另外要注意的是讀寫(xiě)sd卡文件必須首先要在Mainifest.xml文件中配置權(quán)限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
一個(gè)demo,實(shí)現(xiàn)調(diào)用系統(tǒng)相機(jī)拍照,將其顯示在屏幕上,并且存到sd卡。
完整代碼如下:
MyCaremaActivity.java
package barry.android.c;
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)) { // 檢測(cè)sd是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 獲取相機(jī)返回的數(shù)據(jù),并轉(zhuǎn)換為Bitmap圖片格式
FileOutputStream b = null;
File file = new File("/sdcard/myImage/");
file.mkdirs();// 創(chuàng)建文件夾
String fileName = "/sdcard/myImage/111.jpg";
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數(shù)據(jù)寫(xiě)入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);// 將圖片顯示在ImageView里
}
}
}
main.xml
<?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="點(diǎn)擊啟動(dòng)相機(jī)" />
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#999999" />
</LinearLayout>
AndroidMainifest.xml
<?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>
用Intent啟動(dòng)相機(jī)的代碼:
復(fù)制代碼 代碼如下:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);拍完照后就可以在onActivityResult(int requestCode, int resultCode, Intent data)中獲取到Bitmap對(duì)象了。Bitmap bitmap = (Bitmap) data.getExtras().get("data");
要將圖像存儲(chǔ)到sd卡之前最好先檢查一下sd卡是否可用
復(fù)制代碼 代碼如下:
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測(cè)sd是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
以下代碼可以實(shí)現(xiàn)將圖像文件存到“sdcard/myImage/”文件夾下,名稱為“111.jpg”
復(fù)制代碼 代碼如下:
File file = new File("/sdcard/myImage/");
file.mkdirs();// 創(chuàng)建文件夾
String fileName = "/sdcard/myImage/111.jpg";
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數(shù)據(jù)寫(xiě)入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
另外要注意的是讀寫(xiě)sd卡文件必須首先要在Mainifest.xml文件中配置權(quán)限:
復(fù)制代碼 代碼如下:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
一個(gè)demo,實(shí)現(xiàn)調(diào)用系統(tǒng)相機(jī)拍照,將其顯示在屏幕上,并且存到sd卡。
完整代碼如下:
MyCaremaActivity.java
復(fù)制代碼 代碼如下:
package barry.android.c;
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)) { // 檢測(cè)sd是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 獲取相機(jī)返回的數(shù)據(jù),并轉(zhuǎn)換為Bitmap圖片格式
FileOutputStream b = null;
File file = new File("/sdcard/myImage/");
file.mkdirs();// 創(chuàng)建文件夾
String fileName = "/sdcard/myImage/111.jpg";
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數(shù)據(jù)寫(xiě)入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);// 將圖片顯示在ImageView里
}
}
}
main.xml
復(fù)制代碼 代碼如下:
<?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="點(diǎn)擊啟動(dòng)相機(jī)" />
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#999999" />
</LinearLayout>
AndroidMainifest.xml
復(fù)制代碼 代碼如下:
<?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 調(diào)用系統(tǒng)的照相機(jī)和圖庫(kù)實(shí)例詳解
- Android 調(diào)用系統(tǒng)相機(jī)拍攝獲取照片的兩種方法實(shí)現(xiàn)實(shí)例
- Android啟動(dòng)相機(jī)拍照并返回圖片
- Android 7.0調(diào)用相機(jī)崩潰詳解及解決辦法
- Android 簡(jiǎn)單的照相機(jī)程序的實(shí)例代碼
- Android實(shí)現(xiàn)讀取相機(jī)(相冊(cè))圖片并進(jìn)行剪裁
- Android自定義照相機(jī)詳解
- Android自定義相機(jī)實(shí)現(xiàn)自動(dòng)對(duì)焦和手動(dòng)對(duì)焦
- android中打開(kāi)相機(jī)、打開(kāi)相冊(cè)進(jìn)行圖片的獲取示例
- Android仿最新微信相機(jī)功能
相關(guān)文章
Android基于API的Tabs3實(shí)現(xiàn)仿優(yōu)酷t(yī)abhost效果實(shí)例
這篇文章主要介紹了Android基于API的Tabs3實(shí)現(xiàn)仿優(yōu)酷t(yī)abhost效果,結(jié)合完整實(shí)例形式分析了Android實(shí)現(xiàn)優(yōu)酷界面效果的相關(guān)技巧,需要的朋友可以參考下2015-12-12
Android中沒(méi)有插入SD情況下的文件寫(xiě)入和讀取方法
在Android開(kāi)發(fā)時(shí)會(huì)遇到如下一種場(chǎng)合希望應(yīng)用下載到當(dāng)前應(yīng)用的根目錄下,而非SD卡中然后可以隨時(shí)被該應(yīng)用或其他應(yīng)用訪問(wèn)這個(gè)文件,即具有被全局讀取的權(quán)限2012-11-11
Android PopupWindow 點(diǎn)擊外面取消實(shí)現(xiàn)代碼
這篇文章主要介紹了Android PopupWindow 點(diǎn)擊外面取消實(shí)現(xiàn)代碼,本文直接給出核心實(shí)現(xiàn)代碼,代碼中包含注釋,需要的朋友可以參考下2015-04-04
Android編程實(shí)現(xiàn)圖片背景漸變切換與圖層疊加效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片背景漸變切換與圖層疊加效果,涉及Android圖形特效的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Android 3D滑動(dòng)菜單完全解析 Android實(shí)現(xiàn)推拉門(mén)式的立體特效
這篇文章主要為大家詳細(xì)介紹了Android 3D滑動(dòng)菜單,Android實(shí)現(xiàn)推拉門(mén)式的立體特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android之日期時(shí)間選擇控件DatePicker和TimePicker實(shí)例
本篇文章主要介紹了Android之日期時(shí)間選擇控件DatePicker和TimePicker實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-05-05
Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動(dòng)的方法
這篇文章主要介紹了Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動(dòng)的方法,實(shí)例分析了Android中使用libgdx框架實(shí)現(xiàn)響應(yīng)方向鍵的技巧,適用于Android游戲開(kāi)發(fā)領(lǐng)域,需要的朋友可以參考下2015-12-12

