2015-11-26 61 views
0

我有一些问题与设置一个ImageView从画廊意图返回的对象。我一定在做错事,但是当我调试它时,一切似乎都在正确的地方。它不会抛出任何例外,它只是将我的形象改变为无。Android的图像库裁剪图像和设置ImageView

我正试图按照其他指南和教程在stackoverflow得到我的答案,但我不能让它工作。

这个调用画廊很好,但它不允许croping ...

public void onViewCreated(View view, Bundle savedInstanceState) { 
    final ImageView profileImage = (ImageView) view.findViewById(R.id.profile_user_image); 
    profileImage.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(i, RESULT_LOAD_IMAGE); 
     } 
    }); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && null != data) { 
     if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && null != data) { 
      Uri picUri = data.getData(); 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
      Cursor cursor = getActivity().getContentResolver().query(picUri, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      filePath = cursor.getString(columnIndex); 
      cursor.close(); 

        // Set The Bitmap Data To ImageView 
        BitmapFactory.Options o = new BitmapFactory.Options(); 
        o.inPreferredConfig = Bitmap.Config.ARGB_8888; 
        o.inScaled = true; 
        ImageView bitmapview = (ImageView) _this.findViewById(R.id.profile_user_image); 

        bitmapview.setScaleType(ImageView.ScaleType.FIT_XY); 
      //bitmapview.setImageResource(R.drawable.female_icon); // KNOWN FILE 
      bitmapview.setImageBitmap(BitmapFactory.decodeFile(filePath,o)); // Gallery File. 


     } 
    } 
} 

我不知道我缺少什么,在 的ImageView的限制为150dp X 150dp

我已经尝试将其更改为可以正常工作的已知资源,但每次使用此方法都会失败,无论我尝试什么。

回答

0

通过使用com.android.camera.action.CROP在android中裁剪图像。从图库中选择图片网址后。

Intent i = new Intent("com.android.camera.action.CROP"); 
i.setClassName("com.android.camera", "com.android.camera.CropImage"); 
File f = new File(filePath); 
Uri uri = Uri.fromFile(f); 
i.setData(uri); 
i.putExtra("crop", "true"); 
i.putExtra("aspectX", 1); 
i.putExtra("aspectY", 1); 
i.putExtra("outputX", 96); 
i.putExtra("outputY", 96); 
i.putExtra("noFaceDetection", true); 


intent.putExtra("return-data", true);         
startActivityForResult(intent, REQUEST_CROP_ICON); 

当画面选择活动返回将被选择以保存contents.in onActivityResult:

Bundle extras = data.getExtras(); 
if(extras != null) { 
    Bitmap photo = extras.getParcelable("data"); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    photo.compress(Bitmap.CompressFormat.JPEG, 75, stream); 
     // The stream to write to a file or directly using the 
}