十、相機、相簿
1.activity_main.xml:
<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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:text="獲取相機圖片" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="跳轉頁面" />
</RelativeLayout>
2.activity_two:
<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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:text="獲取相簿" />
</RelativeLayout>
3.寫許可權:
<!--許可權 -->
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
4.MainActivity.java:
package com.example.xiangji;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 這個方法可以在Fragment中呼叫
// 4判斷是是不是我們需要的東西
if (requestCode == 1) {
// 5獲取圖片資料
Bitmap bitmap = data.getParcelableExtra("data");
// 6設定圖片
imageView.setImageBitmap(bitmap);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// 【1】設定張相機跳轉意圖(隱式意圖)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 【2】新增意圖
intent.addCategory("android.intent.category.DEFAULT");
// [3]跳轉回傳
startActivityForResult(intent, 1);
break;
case R.id.button2:
startActivity(new Intent(MainActivity.this, TwoActivity.class));
break;
}
}
}
5.TwoActivity.java:
package com.example.xiangji;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class TwoActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
imageView = (ImageView) findViewById(R.id.imageView1);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// [1]設定相簿的意圖
Intent intent1 = new Intent(Intent.ACTION_PICK);
// [2]設定顯式MIME資料型別
intent1.setType("image/*");
// [3]跳轉回傳
startActivityForResult(intent1, 2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//判斷請求碼
if (requestCode==2) {
Uri uri = data.getData();
imageView.setImageURI(uri);
}
}
}