如何从三星的图库中选择图像

How to pick an image from gallery in samsung

我想构建一个应用程序可以从图库中选择照片。这是我使用的代码

Intent photo_pick = new Intent(Intent.ACTION_PICK);
photo_pick.setType("image/*");
startActivityForResult(photo_pick , PICK_PHOTO_INTENT );

这段代码我试过了,它适用于小米、华为 phone。但是当它在三星上工作时它的路径return是错误路径无法获取照片。

如何改进让三星phone也能正常工作?

试试这个代码:

    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    if (intent.resolveActivity(getPackageManager()) != null) {
            // Bring up gallery to select a photo
            startActivityForResult(intent, 2);
       }else{
            Toast.makeText(UserProfileActivity.this,"No Gallery app found!", Toast.LENGTH_SHORT).show();
       }

检查下面的代码以从图库中选择照片,

private static final int REQUEST_PROFILE_ALBUM = 1;


Intent int_album = new Intent(Intent.ACTION_PICK);
int_album.setType("image/*");
int_album.putExtra(MediaStore.EXTRA_OUTPUT, img_url);
startActivityForResult(int_album, REQUEST_PROFILE_ALBUM);

在Select调用Image onActivityResult后,

if (requestCode == REQUEST_PROFILE_ALBUM && resultCode == Activity.RESULT_OK && data != null) {
        Uri selectedImage = data.getData();
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = activity.getContentResolver().query(selectedImage, projection, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(projection[0]);
        String picturePath = cursor.getString(columnIndex); 
}

选择这样的图片

private void showFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }

选择图像后 onActivityResult 会自动调用

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

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

    }
}

试试这个可能对你有帮助:

Button btn_selectimage = (Button) findViewById(R.id.btn_selectimage);

btn_selectimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img_capture.setVisibility(View.VISIBLE);
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
            }
        });

xml的代码:

    <Button
        android:id="@+id/btn_selectimage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Picture"/>

为了执行下面的代码,只有在 android 版本高于 lollipop

时,您才需要获得请求权限
 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
 photoPickerIntent.setType("image/*");
 startActivityForResult(photoPickerIntent, 500);