Select 图片来自画廊 android

Select photo from gallery android

我正在尝试构建一个包含 ImageView 的应用程序。如果我单击它,就会显示图库,这样我就可以 select 为 imageView 添加图片。 我的Xml就是这么简单:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/imageView"
    android:layout_gravity="center_horizontal"
    android:clickable="true"/>
<LinearLayout>

这是我的主要内容 class

public class MainActivity extends Activity {
ImageView img;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    img=(ImageView)findViewById(R.id.imageView);
    setContentView(R.layout.cam);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent =new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Select Contact Image"),1);
        }
    });

 }
 public void onActivityResult(int reqCode,int resCode, Intent data){
    if(resCode==RESULT_OK){
        if(reqCode==1){
            img.setImageURI(data.getData());
        }
    }
  }
}

您的方法是正确的,只需更改几处即可。

这将打开您的画廊:

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

下一步实现 onActivityResult 回调。也很简单:

@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) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

希望对你有所帮助:)

您可以使用神奇的拍照图库。 您可以使用此图库拍摄 select 张照片或拍照,还可以调整照片大小和改变照片质量 :D

1。尝试在 gradle

中编译
 compile 'com.frosquivel:magicaltakephoto:1.0'

2。您在 manifest.xml

中需要此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA"/>

3。例如 class 这样的

// "this"是当前的activity参数

MagicalTakePhoto magicalTakePhoto =  new MagicalTakePhoto(this,ANY_INTEGER_0_TO_4000_FOR_QUALITY);

4。如需拍照请使用方法

magicalTakePhoto.takePhoto("my_photo_name");

5.如果您需要 select 图片在设备中,请尝试使用以下方法:

 magicalTakePhoto.selectedPicture("my_header_name");

6.您需要像这样覆盖 activity 或片段的 onActivityResult 方法:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        magicalTakePhoto.resultPhoto(requestCode, resultCode, data);
        //example to get photo
        //imageView.setImageBitmap(magicalTakePhoto.getMyPhoto());
    }

注意:只有使用此库,您才能在设备中拍摄 select 图片,这需要使用最小 API 15.