20170127 用Intent啟動相機
//20170127 劉彥廷
//用Intent啟動相機
Mainactivity.java
package com.example.user.myapplication47;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Bitmap bmp;
ImageView imv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onGet(View v){
Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(it,100);
}
protected void onActivityResult(int requestCode,int resultCode,
Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(resultCode== Activity.RESULT_OK && requestCode==100){
Bundle extras = data.getExtras(); //將Intent的附加資料轉為Bundle
bmp = (Bitmap)extras.get("data");// 由Bundle取出名為"data"的Bitmap資料
imv =(ImageView)findViewById(R.id.imageView1);
imv.setImageBitmap(bmp); //將Bitmap資料顯示在ImageView中
}
else {
Toast.makeText(this,"沒有拍到照片",Toast.LENGTH_LONG).show();
}
}
}
//用Intent啟動相機
Mainactivity.java
package com.example.user.myapplication47;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Bitmap bmp;
ImageView imv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onGet(View v){
Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(it,100);
}
protected void onActivityResult(int requestCode,int resultCode,
Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(resultCode== Activity.RESULT_OK && requestCode==100){
Bundle extras = data.getExtras(); //將Intent的附加資料轉為Bundle
bmp = (Bitmap)extras.get("data");// 由Bundle取出名為"data"的Bitmap資料
imv =(ImageView)findViewById(R.id.imageView1);
imv.setImageBitmap(bmp); //將Bitmap資料顯示在ImageView中
}
else {
Toast.makeText(this,"沒有拍到照片",Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.user.myapplication47.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:onClick="onGet"
android:text="@string/pic" />
<ImageView
android:layout_width="match_parent"
android:id="@+id/imageView1"
android:scaleType="centerInside"
android:layout_height="match_parent"
android:adjustViewBounds="false"
android:cropToPadding="false" />
</LinearLayout>
strings.xml
<resources>
<string name="app_name">EZ照相機(No47)</string>
<string name="pic">拍照</string>
</resources>
留言
張貼留言