【问题标题】:How to make "Select Image From Gallery or Camera" in Android如何在 Android 中制作“从图库或相机中选择图像”
【发布时间】:2017-07-14 08:38:52
【问题描述】:

我想做“从图库或相机中选择图像”。你可以说互联网上有很多图书馆。我知道,但我还有另一个问题。我可以从图库和相机捕捉中选择图像。但是相机在捕获后导致图像旋转。我solved 旋转的问题。现在我想显示该图像的图像视图。某些设备没有在 imageview 中显示图像(Samsung Note 3 Android 版本 5.0)。我做了很多搜索并找到建议。我应用了这些建议。但我无法解决这个问题。请帮帮我。

【问题讨论】:

    标签: android image select camera gallery


    【解决方案1】:

    同样的问题也发生在我身上。我主要无法在三星和 MI 设备上看到捕获的图像。

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

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

    【讨论】:

      【解决方案2】:

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

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

      第 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 中调用 Dialog。

      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");
          });
      

      就是这样。无需使用任何外部库即可集成自定义“从图库或相机对话框中选择图像”。

      【讨论】:

        【解决方案3】:

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

        如果是Android N 你在 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 位于 resource-xml 文件夹中

        【讨论】:

        • 三星Note 3,安卓5.0版
        • 这不对应问题,它用于共享您的应用程序的文件,而不是选择图像
        【解决方案4】:

        以便用户可以在您的活动中选择图库的图像:

        显示图库的方法:

         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" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-04-27
          • 1970-01-01
          • 2019-09-16
          • 1970-01-01
          • 2017-01-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多