Andrid 呼叫相機拍照,相簿,圖片裁剪
Andrid 呼叫相機拍照,相簿,圖片裁剪
新增讀寫許可權
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
首先是佈局檔案 activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"><de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher" /></LinearLayout>
CircleImageView
是一個載入圓形圖片的控制元件,使用它很簡單隻需要匯入一行依賴:
- implementation 'de.hdodenhof:circleimageview:2.1.0'
緊接著是PopopWindow : popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/pai"
android:text="拍照"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/xuan"
android:text="相簿"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/qu"
android:text="取消"
android:layout_width="match_parent"
android:layout_height="wrap_content" /></LinearLayout>
最後是 MainActivity
package com.example.dell.day02;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity {
// private ImageView img;
private PopupWindow popup;
private LinearLayout lin;
private Button qu;
private Button xuan;
private Button pai;
private Bitmap head;// 頭像Bitmap
private static String path = "/sdcard/myHead/";
private CircleImageView img;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}private void initView() {
lin = (LinearLayout) findViewById(R.id.lin);
// img = (ImageView) findViewById(R.id.img);
img = (CircleImageView) findViewById(R.id.img);
Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");// 從SD卡中找頭像,轉換成Bitmap
if (bt != null) {
@SuppressWarnings("deprecation")
Drawable drawable = new BitmapDrawable(bt);// 轉換成drawable
img.setImageDrawable(drawable);
} else {
/**
* 如果SD裡面沒有則需要從伺服器取頭像,取回來的頭像再儲存在SD中
*
*/
}
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View inflate = getLayoutInflater().inflate(R.layout.popup, null);
popup = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setFocusable(true);
popup.setOutsideTouchable(true);
popup.showAtLocation(lin, Gravity.BOTTOM, 0, 0);
pai = inflate.findViewById(R.id.pai);
xuan = inflate.findViewById(R.id.xuan);
qu = inflate.findViewById(R.id.qu);
pai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "拍照", Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent2.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "head.jpg")));
startActivityForResult(intent2, 2);// 採用ForResult開啟
popup.dismiss();
}
});
xuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "相簿選擇", Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent(Intent.ACTION_PICK, null);
//開啟檔案
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent1, 1);
popup.dismiss();
}
});
qu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_SHORT).show();
popup.dismiss();
}
});
}
});}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
cropPhoto(data.getData());// 裁剪圖片
}break;
case 2:
if (resultCode == RESULT_OK) {
File temp = new File(Environment.getExternalStorageDirectory() + "/head.jpg");
cropPhoto(Uri.fromFile(temp));// 裁剪圖片
}break;
case 3:
if (data != null) {
Bundle extras = data.getExtras();
head = extras.getParcelable("data");
if (head != null) {
/**
* 上傳伺服器程式碼
*/
setPicToView(head);// 儲存在SD卡中
img.setImageBitmap(head);// 用ImageButton顯示出來
}
}
break;
default:
break;}
super.onActivityResult(requestCode, resultCode, data);
}/**
* 呼叫系統的裁剪功能
*
* @param uri
*/
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是寬高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪圖片寬高
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
}private void setPicToView(Bitmap mBitmap) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測sd是否可用
return;
}
FileOutputStream b = null;
File file = new File(path);
file.mkdirs();// 建立資料夾
String fileName = path + "head.jpg";// 圖片名字
try {
b = new FileOutputStream(fileName);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把資料寫入檔案
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
// 關閉流
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Android 7.0,8.0 手機開啟照相機會崩潰 請關注我的另一篇部落格 裡面有很詳細的解決7.0 8.0相機奔潰問題