如何在 Android 中制作 "Select Image From Gallery or Camera"

How to make "Select Image From Gallery or Camera" in Android

我想"Select Image From Gallery or Camera"。你可以说互联网上有很多这方面的图书馆。我知道,但我还有另一个问题。我可以从画廊和相机捕捉中制作 select 图像。但是相机导致图像在捕获后旋转。我solved旋转的问题。现在我想显示一个 imageview 该图像。某些设备没有在 imageview 中显示图像(Samsung Note 3 Android 5.0 版)。我进行了大量搜索并找到了建议。我采纳了这些建议。但我无法解决那个问题。请帮助我。

哪个版本的 android 图片没有显示在 imageview 中

如果是AndroidN 您在 Manifest

中提供提供商

将此代码添加到您的清单中

  <application
        android:name=".MyApplication"
        android:theme="@style/MyAppTheme">
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

provider_paths 位于资源-xml 文件夹中

我也遇到了同样的问题。我大多无法在 Samsung 和 MI 设备上看到捕获的图像。

通过使用这个库,我解决了我的问题。

https://github.com/coomar2841/android-multipicker-library

以便用户可以选择图库中的图片,在您的 Activity 中执行此操作:

展示图库的方法:

 private static final int SELECT_PHOTO = 1;

 public void showGallery() {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");       
        startActivityForResult(photoPickerIntent, SELECT_PHOTO);
    }

当用户选择一张图片时会调用此方法,如果有图片路径,将存储在imagePath

private String imagePath;

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            imagePath = "";
            if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);

                if (cursor != null) {
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    imagePath = cursor.getString(columnIndex);
                    cursor.close();
                    //Here you can call a method that loads the image and 
                    //show it in an ImageView
                }
            }
        }

您需要在清单中获得许可:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

现在回答可能为时已晚,但我希望它能对其他人有所帮助。

与其使用外部库,不如创建自己的布局并使用它。

第 1 步:为选项创建布局。

image_choose_bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvClickImageText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_vector_camera_icon"
            android:gravity="center"
            android:padding="10dp"
            android:text="Take Photo"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toStartOf="@+id/tvChooseGalleryText"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <TextView
            android:id="@+id/tvChooseGalleryText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_vector_gallery_icon"
            android:gravity="center"
            android:padding="10dp"
            android:text="Choose Image"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/tvClickImageText"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tvClickImageText"
            app:layout_constraintTop_toTopOf="@+id/tvClickImageText" />

    </android.support.constraint.ConstraintLayout>

</layout>

第 2 步:创建一个 BottomSheetDialog,您可以在其中扩充布局并进行配置。

DialogImageChooser.java

public class DialogImageChooser extends BottomSheetDialogFragment {

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

View view;
//    DialogNationalitySelectionBinding mBinding;
Context mContext;
private bottomSheetListener mListener;
TextView tvClickImageText, tvChooseGalleryText;

public DialogImageChooser() {
}

@SuppressLint("ValidFragment")
public DialogImageChooser(@NonNull Context context, bottomSheetListener mListener) {
    this.mListener = mListener;
    mContext = context;
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    view = inflater.inflate(R.layout.image_choose_bottom_sheet, container, false);

    tvClickImageText = view.findViewById(R.id.tvClickImageText);
    tvChooseGalleryText = view.findViewById(R.id.tvChooseGalleryText);

    tvClickImageText.setOnClickListener(v -> {
        mListener.onCardClicked("camera");
        dismiss();
    });

    tvChooseGalleryText.setOnClickListener(v -> {
        mListener.onCardClicked("gallery");
        dismiss();
    });

    return view;
}


public interface bottomSheetListener {
    void onCardClicked(String option);
}
}

第 3 步:最后在 Activity 中调用对话框。

YourActivity.java

ivItemImage.setOnClickListener(v -> {
        new DialogImageChooser(YourActivity.this, selectedOption -> {
            if (selectedOption.equalsIgnoreCase("camera")) {
                TakePictureIntent(CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
            } else if (selectedOption.equalsIgnoreCase("gallery")) {
                Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, SELECT_IMAGE);
            }
        }).show(getSupportFragmentManager(), "add_photo_dialog");
    });

就是这样。自定义 "Select Image From Gallery or Camera Dialog" 是在不使用任何外部库的情况下集成的。