解决大图片裁剪问题的详细代码(可直接粘贴复用)

解决大图片裁剪问题的详细代码(可直接粘贴复用)

MainActivity中的代码

package com.mzba.photocrop;


import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;


import android.app.Activity;

import android.app.AlertDialog;

import android.content.ActivityNotFoundException;

import android.content.Context;

import android.content.DialogInterface;

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.ContextThemeWrapper;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.ListAdapter;

import android.widget.TextView;

/**

 * Android大图裁剪解决办法

 * @author  06peng

 * @see  http://06peng.com

 *

 */

public class MainActivity extends Activity {

private ImageView imageView;

private TextView textView;

private File picFile;

private Uri photoUri;

private final int activity_result_camara_with_data = 1006;

private final int activity_result_cropimage_with_data = 1007;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView = (ImageView) findViewById(R.id.image);

textView = (TextView) findViewById(R.id.click);

textView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

doPickPhotoAction();

}

});

}


private void doPickPhotoAction() {

final Context dialogContext = new ContextThemeWrapper(this, android.R.style.Theme_Light);

String cancel = "Back";

String[] choices;

choices = new String[2];

choices[0] = "Take Photo"; // 拍照

choices[1] = "Choose Photo"; // 从相册中选择

final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,

android.R.layout.simple_list_item_1, choices);


final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);

builder.setTitle("Choose Photo");

builder.setSingleChoiceItems(adapter, -1,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

switch (which) {

case 0:

String status = Environment.getExternalStorageState();

if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡

doTakePhoto();// 用户点击了从照相机获取

}

break;

case 1:

doCropPhoto();// 从相册中去获取

break;

}

}

});

builder.setNegativeButton(cancel,

new DialogInterface.OnClickListener() {


@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}


});

builder.create().show();

}


/**

* 拍照获取图片

*/

protected void doTakePhoto() {

try {

File uploadFileDir = new File(Environment.getExternalStorageDirectory(), "/upload");

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (!uploadFileDir.exists()) {

uploadFileDir.mkdirs();

}

picFile = new File(uploadFileDir, "upload.jpeg");

if (!picFile.exists()) {

picFile.createNewFile();

}

photoUri = Uri.fromFile(picFile);

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

startActivityForResult(cameraIntent, activity_result_camara_with_data);

} catch (ActivityNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

protected void doCropPhoto() {

try {

File pictureFileDir = new File(Environment.getExternalStorageDirectory(), "/upload");

if (!pictureFileDir.exists()) {

pictureFileDir.mkdirs();

}

picFile = new File(pictureFileDir, "upload.jpeg");

if (!picFile.exists()) {

picFile.createNewFile();

}

photoUri = Uri.fromFile(picFile);

final Intent intent = getCropImageIntent();

startActivityForResult(intent, activity_result_cropimage_with_data);

} catch (Exception e) {

e.printStackTrace();

}

}


/**

* Constructs an intent for image cropping. 调用图片剪辑程序

*/

public Intent getCropImageIntent() {

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);

intent.setType("image/*");

intent.putExtra("crop", "true");

intent.putExtra("aspectX", 1);

intent.putExtra("aspectY", 1);

intent.putExtra("outputX", 600);

intent.putExtra("outputY", 600);

intent.putExtra("noFaceDetection", true);

intent.putExtra("scale", true);

intent.putExtra("return-data", false);

intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

return intent;

}

private void cropImageUriByTakePhoto() {

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(photoUri, "image/*");

intent.putExtra("crop", "true");

intent.putExtra("aspectX", 1);

intent.putExtra("aspectY", 1);

intent.putExtra("outputX", 600);

intent.putExtra("outputY", 600);

intent.putExtra("scale", true);

intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

intent.putExtra("return-data", false);

intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

intent.putExtra("noFaceDetection", true); // no face detection

startActivityForResult(intent, activity_result_cropimage_with_data);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) {

case activity_result_camara_with_data: // 拍照

try {

cropImageUriByTakePhoto();

} catch (Exception e) {

e.printStackTrace();

}

break;

case activity_result_cropimage_with_data:

try {

if (photoUri != null) {

Bitmap bitmap = decodeUriAsBitmap(photoUri);

imageView.setImageBitmap(bitmap);

}

} catch (Exception e) {

e.printStackTrace();

}

break;

}

}

private Bitmap decodeUriAsBitmap(Uri uri) {

Bitmap bitmap = null;

try {

bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

} catch (FileNotFoundException e) {

e.printStackTrace();

return null;

}

return bitmap;

}

}

在清单文件中:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.mzba.photocrop"

    android:versionCode="1"

    android:versionName="1.0" >


    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.mzba.photocrop.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,大图片裁剪,详细代码)