如何将上传图像的形状(图片从照片库)更改为圆形

时间:2015-08-02 10:59:23

标签: android json eclipse image-gallery

如何在Android eclipse中将上传图像的形状(图片从照片库中)更改为圆形。

1 个答案:

答案 0 :(得分:0)

这是您使用它的方式:

//this the button which allows you to access the gallery
pic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gallery();
        }
    });

//this allows you select one image from gallery
 public void gallery() {
    intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
//when you start activity for result and choose an image, the code will automatically continue here
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            Uri current_ImageURI = data.getData();
            selectedImagePath = getPath(current_ImageURI);
            image.setImageDrawable(RoundedBitmapDrawableFactory.create(getResources(),selectedImagePath));
        } 
    }
}

public String getPath(Uri contentUri) {
// we have to check for sdk version because from lollipop the retrieval of path is different 
        if (Build.VERSION.SDK_INT < 21) {
            // can post image
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();

            return cursor.getString(column_index);
        } else {
            String filePath = "";
            String wholeID = DocumentsContract.getDocumentId(contentUri);

            // Split at colon, use second item in the array
            String id = wholeID.split(":")[1];

            String[] column = {MediaStore.Images.Media.DATA};

            // where id is equal to
            String sel = MediaStore.Images.Media._ID + "=?";

            Cursor cursor = getActivity().getContentResolver().
                    query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            column, sel, new String[]{id}, null);


            int columnIndex = cursor.getColumnIndex(column[0]);

            if (cursor.moveToFirst()) {
                filePath = cursor.getString(columnIndex);
            }
            cursor.close();
            return filePath;
        }
    }

现在有以下两个功能: 解码位图的样本大小(做正确的缩放),另一个从路径中提取位图也不再使用

不再使用循环位图类

为了更好地理解,请使用此链接参考:http://developer.android.com/reference/android/support/v4/graphics/drawable/RoundedBitmapDrawableFactory.html