Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Java喬曉松-android中調用系統拍照功能並顯示拍照的圖片

Java喬曉松-android中調用系統拍照功能並顯示拍照的圖片

編輯:關於Android編程

android中調用系統拍照功能並顯示拍照的圖片

如果你是拍照完,利用onActivityResult獲取data數據,把data數據轉換成Bitmap數據,這樣獲取到的圖片,是拍照的照片的縮略圖

代碼如下:

[html]
package com.example.myphotos; 
 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.net.Uri; 
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; 
 
/** 
 * 2013-6-27 上午10:27:23 
 *  
 * @author 喬曉松 
 */ 
public class CameraActivity extends Activity { 
 
    private Button button; 
    private ImageView imageView; 
    private String fileName; 
 
    @SuppressLint({ "SimpleDateFormat", "SdCardPath" }) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        this.setContentView(R.layout.activity_camera); 
        button = (Button) findViewById(R.id.btn_camera); 
        imageView = (ImageView) findViewById(R.id.imageView1); 
        File file = new File("/sdcard/myImage/"); 
        file.mkdirs();// 創建文件夾 
 
        button.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
                                startActivityForResult(intent, 1); 
            } 
        }); 
        // Intent intent = new Intent(); 
        // intent.setAction("android.intent.action.MAIN"); 
        // intent.addCategory("android.intent.category.LAUNCHER"); 
        // intent.setFlags(0x10200000); 
        // intent.setComponent(new ComponentName("com.android.camera", 
        // "com.android.camera.Camera")); 
        // startActivity(intent); 
    } 
 
    @SuppressLint({ "SdCardPath", "SimpleDateFormat" }) 
    @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; 
            } 
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");String name = format.format(new Date());fileName = "/sdcard/myImage/" + name + ".jpg"; 
            Bundle bundle = data.getExtras(); 
            Bitmap bitmap = (Bitmap) bundle.get("data");// 獲取相機返回的數據,並轉換為Bitmap圖片格式 
            FileOutputStream b = null; 
 
            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.setImageBitmap(bitmap);// 將圖片顯示在ImageView裡 
        } 
 
    } 

package com.example.myphotos;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
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;

/**
 * 2013-6-27 上午10:27:23
 *
 * @author 喬曉松
 */
public class CameraActivity extends Activity {

 private Button button;
 private ImageView imageView;
 private String fileName;

 @SuppressLint({ "SimpleDateFormat", "SdCardPath" })
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.setContentView(R.layout.activity_camera);
  button = (Button) findViewById(R.id.btn_camera);
  imageView = (ImageView) findViewById(R.id.imageView1);
  File file = new File("/sdcard/myImage/");
  file.mkdirs();// 創建文件夾

  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 1);
   }
  });
  // Intent intent = new Intent();
  // intent.setAction("android.intent.action.MAIN");
  // intent.addCategory("android.intent.category.LAUNCHER");
  // intent.setFlags(0x10200000);
  // intent.setComponent(new ComponentName("com.android.camera",
  // "com.android.camera.Camera"));
  // startActivity(intent);
 }

 @SuppressLint({ "SdCardPath", "SimpleDateFormat" })
 @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;
   }
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");String name = format.format(new Date());fileName = "/sdcard/myImage/" + name + ".jpg";
   Bundle bundle = data.getExtras();
   Bitmap bitmap = (Bitmap) bundle.get("data");// 獲取相機返回的數據,並轉換為Bitmap圖片格式
   FileOutputStream b = null;

   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.setImageBitmap(bitmap);// 將圖片顯示在ImageView裡
  }

 }
}
上面獲取到的拍攝照片的縮略圖,要想獲取到拍攝照片的原圖,就要在打開相機的時候,把照片保存的地址必須設置好,代碼如下:

[html]
package com.example.myphotos; 
 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.net.Uri; 
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; 
 
/** 
 * 2013-6-27 上午10:27:23 
 *  
 * @author 喬曉松 
 */ 
public class CameraActivity extends Activity { 
 
    private Button button; 
    private ImageView imageView; 
    private String fileName; 
 
    @SuppressLint({ "SimpleDateFormat", "SdCardPath" }) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        this.setContentView(R.layout.activity_camera); 
        button = (Button) findViewById(R.id.btn_camera); 
        imageView = (ImageView) findViewById(R.id.imageView1); 
        File file = new File("/sdcard/myImage/"); 
        file.mkdirs();// 創建文件夾 
 
        button.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                SimpleDateFormat format = new SimpleDateFormat( 
                        "yyyy-MM-dd HH.mm.ss"); 
                String name = format.format(new Date()); 
                fileName = "/sdcard/myImage/" + name + ".jpg"; 
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
                intent.putExtra(MediaStore.EXTRA_OUTPUT, 
                        Uri.fromFile(new File(fileName))); 
                startActivityForResult(intent, 1); 
            } 
        }); 
        // Intent intent = new Intent(); 
        // intent.setAction("android.intent.action.MAIN"); 
        // intent.addCategory("android.intent.category.LAUNCHER"); 
        // intent.setFlags(0x10200000); 
        // intent.setComponent(new ComponentName("com.android.camera", 
        // "com.android.camera.Camera")); 
        // startActivity(intent); 
    } 
 
    @SuppressLint({ "SdCardPath", "SimpleDateFormat" }) 
    @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; 
 
            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.setImageBitmap(bitmap);// 將圖片顯示在ImageView裡 
        } 
 
    } 

package com.example.myphotos;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
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;

/**
 * 2013-6-27 上午10:27:23
 *
 * @author 喬曉松
 */
public class CameraActivity extends Activity {

 private Button button;
 private ImageView imageView;
 private String fileName;

 @SuppressLint({ "SimpleDateFormat", "SdCardPath" })
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.setContentView(R.layout.activity_camera);
  button = (Button) findViewById(R.id.btn_camera);
  imageView = (ImageView) findViewById(R.id.imageView1);
  File file = new File("/sdcard/myImage/");
  file.mkdirs();// 創建文件夾

  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    SimpleDateFormat format = new SimpleDateFormat(
      "yyyy-MM-dd HH.mm.ss");
    String name = format.format(new Date());
    fileName = "/sdcard/myImage/" + name + ".jpg";
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
      Uri.fromFile(new File(fileName)));
    startActivityForResult(intent, 1);
   }
  });
  // Intent intent = new Intent();
  // intent.setAction("android.intent.action.MAIN");
  // intent.addCategory("android.intent.category.LAUNCHER");
  // intent.setFlags(0x10200000);
  // intent.setComponent(new ComponentName("com.android.camera",
  // "com.android.camera.Camera"));
  // startActivity(intent);
 }

 @SuppressLint({ "SdCardPath", "SimpleDateFormat" })
 @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;

   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.setImageBitmap(bitmap);// 將圖片顯示在ImageView裡
  }

 }
}
 

 

布局文件代碼如下:

[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 
 
    <Button 
        android:id="@+id/btn_camera" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentTop="true" 
        android:text="@string/btn_carema" /> 
 
    <ImageView 
        android:id="@+id/imageView1" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_alignLeft="@+id/btn_camera" 
        android:layout_below="@+id/btn_camera" 
        android:layout_marginTop="17dp" 
        android:background="#999999" 
        tools:ignore="ContentDescription" /> 
 
</RelativeLayout> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/btn_carema" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/btn_camera"
        android:layout_below="@+id/btn_camera"
        android:layout_marginTop="17dp"
        android:background="#999999"
        tools:ignore="ContentDescription" />

</RelativeLayout>
 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved