Androidはカメラを呼び出してsdカードに写真を保存します
7216 ワード
最近のプロジェクトではカメラで写真を撮ることになっていますが、インターネットで調べてみると、Androidで写真を撮るには2つの方法があります.1つは、システムが持っているカメラを呼び出し、戻ってきた写真データを使うことです.もう1つは、カメラ機能を自分でCamera類や他の関連類で実現する方法で、制度が高く、洗染も複雑で、一般的に通常の応用は1つ目を使うだけでよい.Intentでカメラを起動するコード:
写真を撮るとonActivity Result(int requestCode,int resultCode,Intent data)でBitmapオブジェクトを取得できます.
画像をsdカードに格納する前に、sdカードが使用可能かどうかを確認したほうがいいです.
以下のコードは、画像ファイルを「111.jpg」という名前の「sdcard/myImage/」フォルダに保存することを実現します.
また、sdカードのファイルを読み書きするには、まずMainifestでなければならないことに注意してください.xmlファイルでの権限の構成:
システムカメラを呼び出して写真を撮り、画面に表示してsdカードに保存するdemo.
完全なコードは次のとおりです.
MyCaremaActivity.java
main.xmlコードは次のとおりです.
AndroidMainifest.xmlコードは次のとおりです.
よしOK!
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
写真を撮るとonActivity Result(int requestCode,int resultCode,Intent data)でBitmapオブジェクトを取得できます.
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
画像をsdカードに格納する前に、sdカードが使用可能かどうかを確認したほうがいいです.
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // sd
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
以下のコードは、画像ファイルを「111.jpg」という名前の「sdcard/myImage/」フォルダに保存することを実現します.
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();
}
}
また、sdカードのファイルを読み書きするには、まずMainifestでなければならないことに注意してください.xmlファイルでの権限の構成:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
システムカメラを呼び出して写真を撮り、画面に表示してsdカードに保存するdemo.
完全なコードは次のとおりです.
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)) { // 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
}
}
}
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=" " />
<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>
よしOK!