在此知識點上筆者也是花了大量的時間,由于android版本的更新,android4.4和android4.3在OnActivityResult中返回的uri是完全不同的,所以也造成了筆者的學習資料中的demo不能正常運行的情況,也是糾結了大半天。
但是在此博文中也是對其中細節不做介紹了,還是把正確的代碼展示給大家把。
主界面:

選擇相冊中的照片:

選擇照片后的裁剪界面:

完成后效果:

MainActivity:
~~~
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final int CUT_PICTURE = 1;
public static final int SHOW_PICTURE = 2;
private Button takePhoto;
private Button chooseFromAlbum;
private ImageView picture;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePhoto = (Button) findViewById(R.id.take_photo);
chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
picture = (ImageView) findViewById(R.id.picture);
takePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//創建File對象,用于存儲拍照后的圖片
//將此圖片存儲于SD卡的根目錄下
File outputImage = new File(Environment.getExternalStorageDirectory(),
"output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//將File對象轉換成Uri對象
//Uri表標識著圖片的地址
imageUri = Uri.fromFile(outputImage);
//隱式調用照相機程序
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//拍下的照片會被輸出到output_image.jpg中去
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//此處是使用的startActivityForResult()
//因此在拍照完后悔有結果返回到onActivityResult()中去,返回值即為<span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;">CUT_PICTURE</span>
//onActivityResult()中主要是實現圖片裁剪
startActivityForResult(intent, CUT_PICTURE);
}
});
chooseFromAlbum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File outputImage = new File(Environment.getExternalStorageDirectory(),
"output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent(Intent.ACTION_PICK,null);
//此處調用了圖片選擇器
//如果直接寫intent.setDataAndType("image/*");
//調用的是系統圖庫
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CUT_PICTURE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CUT_PICTURE:
if (resultCode == RESULT_OK) {
//此處啟動裁剪程序
Intent intent = new Intent("com.android.camera.action.CROP");
//此處注釋掉的部分是針對android 4.4路徑修改的一個測試
//有興趣的讀者可以自己調試看看
// String text=data.getData().toString();
// Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
intent.setDataAndType(data.getData(), "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, SHOW_PICTURE);
}
break;
case SHOW_PICTURE:
if (resultCode == RESULT_OK) {
try {
//將output_image.jpg對象解析成Bitmap對象,然后設置到ImageView中顯示出來
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
~~~
activity_main:
~~~
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Take Photo" />
<Button
android:id="@+id/choose_from_album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose From Album" />
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
~~~
AndroidManifest:
~~~
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.choosepictest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.choosepictest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
~~~
- 前言
- android SharedPreference的簡單使用(登陸界面記住密碼)
- android 通知Notification的使用小實例(振動,燈光,聲音)
- android調用攝像頭拍照,從相冊中選擇照片并裁剪
- android從相冊中獲取圖片出錯,圖片無法裁剪的問題
- android 后臺定時提醒(Service,AlarmManager的使用)
- android如何讓后臺服務service不被殺死(設置前臺服務)
- android修改控件外觀(使用drawable資源)
- android 自定義view的使用(最佳demo——返回標題欄)
- android viewpager+fragment做主界面(超容易理解的demo!)
- Fragment生命周期
- Android DrawerLayout 高仿QQ5.2雙向側滑菜單
- Android DrawerLayout側滑菜單+nineoldandroids動畫
- Android 通知欄Notification的整合 全面學習 (一個DEMO讓你完全了解它)
- Android應用開發-護眼提醒-總結篇