Androidアップロード画像トリミング機能


    ,     ,       :PicCutDemoActivity.java
package com.superspace;


import java.io.File;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;


/**
 * @Title: PicCutDemoActivity.java
 * @Package com.superspace
 * @Description:         
 * @author SuperSpace
 */
public class PicCutDemoActivity extends Activity implements OnClickListener {


 private ImageButton ib = null;
 private ImageView iv = null;
 private Button bt = null;
 private String tp = null;


 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 //    
 init();
 }


 /**
 *        
 */
 private void init() {
 bt = (Button) findViewById(R.id.bt);
 ib = (ImageButton) findViewById(R.id.ib);
 iv = (ImageView) findViewById(R.id.iv);


 bt.setOnClickListener(this);
 ib.setOnClickListener(this);
 iv.setOnClickListener(this);
 }


 /**
 *                              ,              ,                      
 */
 public void onClick(View v) {
 switch (v.getId()) {
 case R.id.bt:
 ShowPickDialog();
 break;
 case R.id.ib:
 ShowPickDialog();
 break;
 case R.id.iv:
 ShowPickDialog();
 break;


 default:
 break;
 }
 }


 /**
 *        
 */
 private void ShowPickDialog() {
 new AlertDialog.Builder(this)
 .setTitle("    ...")
 .setNegativeButton("  ", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int which) {
 dialog.dismiss();
 /**
 *    ,       ACTION_PICK    ,     Intent  ,
 *           ,Intent        ,         
 */
 Intent intent = new Intent(Intent.ACTION_PICK, null);


 /**
 *      ,            ,  :
 * intent.setData(MediaStore.Images
 * .Media.EXTERNAL_CONTENT_URI);
 * intent.setType(""image/*");      
 *                           
 * :"image/jpeg 、 image/png    "
 */
 intent.setDataAndType(
 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
 "image/*");
 startActivityForResult(intent, 1);
 }
 })
 .setPositiveButton("  ", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int whichButton) {
 dialog.dismiss();
 /**
 *          ,        ,          ,          
 *   ,you_sdk_path/docs/guide/topics/media/camera.html
 *               ,     ,          ,        
 *            ,     
 */
 Intent intent = new Intent(
 MediaStore.ACTION_IMAGE_CAPTURE);
 //                      
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
 .fromFile(new File(Environment
 .getExternalStorageDirectory(),
 "superspace.jpg")));
 startActivityForResult(intent, 2);
 }
 }).show();
 }


 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 switch (requestCode) {
 //           
 case 1:
 startPhotoZoom(data.getData());
 break;
 //           
 case 2:
 File temp = new File(Environment.getExternalStorageDirectory()
 + "/superspace.jpg");
 startPhotoZoom(Uri.fromFile(temp));
 break;
 //         
 case 3:
 /**
 *            ,       ,             ,     ,  
 *      ,  NullException,         ,                          
 * 
 */
 if (data != null) {
 setPicToView(data);
 }
 break;
 default:
 break;


 }
 super.onActivityResult(requestCode, resultCode, data);
 }


 /**
 *         
 * 
 * @param uri
 */


 public void startPhotoZoom(Uri uri) {
 /*
 *       Intent ACTION      ,                
 * yourself_sdk_path/docs/reference/android/content/Intent.html
 *      Ctrl+F :CROP ,       ,                  ,         
 */
 Intent intent = new Intent("com.android.camera.action.CROP");
 intent.setDataAndType(uri, "image/*");
 //     crop=true       Intent      VIEW   
 intent.putExtra("crop", "true");
 // aspectX aspectY       
 intent.putExtra("aspectX", 1);
 intent.putExtra("aspectY", 1);
 // outputX outputY        
 intent.putExtra("outputX", 150);
 intent.putExtra("outputY", 150);
 intent.putExtra("return-data", true);
 startActivityForResult(intent, 3);
 }


 /**
 *            
 * 
 * @param picdata
 */
 private void setPicToView(Intent picdata) {
 Bundle extras = picdata.getExtras();
 if (extras != null) {
 Bitmap photo = extras.getParcelable("data");
 Drawable drawable = new BitmapDrawable(photo);


 /**
 *                  Base64Coder            ,QQ              
 */


 /*
 * ByteArrayOutputStream stream = new ByteArrayOutputStream();
 * photo.compress(Bitmap.CompressFormat.JPEG, 60, stream); byte[] b
 * = stream.toByteArray(); //               
 * 
 * tp = new String(Base64Coder.encodeLines(b));
 *                      ,   tp        ,                  ,  
 * 
 *                Base64Coder     ,                      OK ...  
 * Bitmap dBitmap = BitmapFactory.decodeFile(tp); Drawable drawable
 * = new BitmapDrawable(dBitmap);
 */
 ib.setBackgroundDrawable(drawable);
 iv.setBackgroundDrawable(drawable);
 }
 }


}  
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id ="@+id/test01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button
    android:id="@+id/bt"
      android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="    "
    />
<ImageButton
    android:id="@+id/ib"
      android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher"
    />
<ImageView android:id="@+id/iv"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher"/>


</LinearLayout>