【问题标题】:android:select image from gallery then crop that and show in an imageviewandroid:从图库中选择图像,然后裁剪并显示在图像视图中
【发布时间】:2014-10-18 21:35:41
【问题描述】:

我真的需要这个代码,我在网上搜索了 3 个小时,但我找不到完整而简单的代码,我测试了很多代码,但有些不起作用,有些不好,请帮助我完整而简单的代码,谢谢 编辑:我有这个选择图像的代码,但请给我一个完整的代码,因为我不能像代码一样。

  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);
                    }

【问题讨论】:

    标签: android imageview gallery crop


    【解决方案1】:

    希望本准则对您有所帮助..

    从图库中选择图像的活动。

    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;
            }
        }
    }
    

    裁剪所选图像的活动:

    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();
            }
        }   
    }
    

    编辑: 签出此开源库以从图库中选择图像或使用相机捕获新图像。它将节省大量样板代码。 https://github.com/Dhaval2404/ImagePicker

    【讨论】:

    • 如果您得到解决方案,请接受答案。
    • 感谢分享。但是,我必须注意 both 这些活动必须在清单中具有 android:launchMode="singleTop"android:noHistory="false" 属性。否则它不起作用并且onActivityResultRESULT_CANCELED 调用。我花了好几个小时才弄明白。
    • 另外,我们不需要为图像选择和裁剪设置单独的活动。它可以在一个单一的活动中完成。
    • 是的 @Atul 是正确的,不需要 2 个单独的活动。
    • 我在 Bundle extras = data.getExtras(); 处遇到空指针异常。 extras 变量为空。
    【解决方案2】:

    我在这里发布工作代码:

    private static final int PICK_FROM_GALLERY = 2;
    
    buttonGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
          //******call android default gallery
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
          //******code for crop image
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 0);
                intent.putExtra("aspectY", 0);
                try {
                   intent.putExtra("return-data", true);
                   startActivityForResult(
                      Intent.createChooser(intent,"Complete action using"),
                      PICK_FROM_GALLERY);
                } catch (ActivityNotFoundException e) {}
            }
        });
    }
    

    onActivityResult

    if (requestCode == PICK_FROM_GALLERY) {
        Bundle extras2 = data.getExtras();
        if (extras2 != null) {
                Bitmap photo = extras2.getParcelable("data");
                imgview.setImageBitmap(photo);
        }
    }
    

    请试试这个。

    【讨论】:

    • 非常适合我。唯一的问题是图像分辨率非常低。有什么办法解决这个问题?
    • 完成操作使用什么?
    • @siddhartha shankar,这个方法需要存储权限吗
    【解决方案3】:

    我有一点不同的方法

    我使用external library 管理我的下载 库会在后台加载它

    此设置允许具有大量项目和快速滚动的列表具有良好的性能:)

    DisplayImageOptions options=new DisplayImageOptions.Builder()                       .cacheOnDisk(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .imageScaleType(ImageScaleType.EXACTLY)     
            .build();
    
    ImageLoader.getInstance().displayImage(url, imageView, options);
    

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      相关资源
      最近更新 更多