android:从图库中选择图像,然后裁剪并显示在图像视图中

android:select image from gallery then crop that and show in an imageview(android:从图库中选择图像,然后裁剪并显示在图像视图中)

本文介绍了android:从图库中选择图像,然后裁剪并显示在图像视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i really need this code and i searched for 3 hours on internet but i couldn't find a complete and simple code and i tested many codes but some didn't work and others wasn't good,please help me with a full and simple code,thank you edit:i have this code for select image but please give me a full code for all things that i said in title because i cant resemble codes.

  btn_choose.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                   Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
       startActivityForResult(Intent.createChooser(intent,"Select Picture"),SELECT_PICTURE);


            alertDialog1.dismiss();

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

       if (resultCode == RESULT_OK) {

   if (requestCode == SELECT_PICTURE) {
                        Uri selectedImageUri = data.getData();
                        selectedImagePath = getPath(selectedImageUri);
                        selectedImagePath1=selectedImagePath;
                        System.out.println("Image Path : " + selectedImagePath);
                        image_capture1.setImageURI(selectedImageUri);
                    }

解决方案

Hope This Code will help you..

Activity To Select The Image From Gallery.

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

public class GalleryUtil extends Activity{
    private final static int RESULT_SELECT_IMAGE = 100;
    public static final int MEDIA_TYPE_IMAGE = 1;
    private static final String TAG = "GalleryUtil";

    String mCurrentPhotoPath;
    File photoFile = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
            //Pick Image From Gallery
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_SELECT_IMAGE); 
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode){
        case RESULT_SELECT_IMAGE:

            if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
                try{
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA };
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                //return Image Path to the Main Activity
                Intent returnFromGalleryIntent = new Intent();
                returnFromGalleryIntent.putExtra("picturePath",picturePath);
                setResult(RESULT_OK,returnFromGalleryIntent);     
                finish();
                }catch(Exception e){
                    e.printStackTrace();
                    Intent returnFromGalleryIntent = new Intent();
                    setResult(RESULT_CANCELED, returnFromGalleryIntent);     
                    finish();   
                }
            }else{
                Log.i(TAG,"RESULT_CANCELED");     
                Intent returnFromGalleryIntent = new Intent();
                setResult(RESULT_CANCELED, returnFromGalleryIntent);     
                finish();
            }
            break;
        }
    }
}

Activity To Crop The Selected Image:

public class ImageSelecter extends Activity{

    private final int GALLERY_ACTIVITY_CODE=200;
    private final int RESULT_CROP = 400;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btn_choose.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Start Activity To Select Image From Gallery   
                Intent gallery_Intent = new Intent(getApplicationContext(), GalleryUtil.class);
                startActivityForResult(gallery_Intent, GALLERY_ACTIVITY_CODE);
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_ACTIVITY_CODE) {
             if(resultCode == Activity.RESULT_OK){  
                 picturePath = data.getStringExtra("picturePath");  
                 //perform Crop on the Image Selected from Gallery
                 performCrop(picturePath);
             }
        }

        if (requestCode == RESULT_CROP ) {
             if(resultCode == Activity.RESULT_OK){  
                 Bundle extras = data.getExtras();
                 Bitmap selectedBitmap = extras.getParcelable("data");
                 // Set The Bitmap Data To ImageView
                 image_capture1.setImageBitmap(selectedBitmap);                             
                 image_capture1.setScaleType(ScaleType.FIT_XY);
             }
        }
    }

    private void performCrop(String picUri) {
        try {
            //Start Crop Activity

            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            // indicate image type and Uri
            File f = new File(picUri);
            Uri contentUri = Uri.fromFile(f);

            cropIntent.setDataAndType(contentUri, "image/*");
            // set crop properties
            cropIntent.putExtra("crop", "true");
            // indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            // indicate output X and Y
            cropIntent.putExtra("outputX", 280);
            cropIntent.putExtra("outputY", 280);

            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, RESULT_CROP);
        }
        // respond to users whose devices do not support the crop action
        catch (ActivityNotFoundException anfe) {
            // display an error message
            String errorMessage = "your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }   
}

Edit: Checkout this open source library to Pick image from Gallery or Capture new image with Camera. It will save lot of boilerplate code. https://github.com/Dhaval2404/ImagePicker

这篇关于android:从图库中选择图像,然后裁剪并显示在图像视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:android:从图库中选择图像,然后裁剪并显示在图像视图中

基础教程推荐