【问题标题】:Choose a picture from the gallery only, not other application like Photos App仅从图库中选择图片,而不是其他应用程序,例如 Photos App
【发布时间】:2015-08-23 17:46:13
【问题描述】:

我正在尝试仅从 Android 图库中选择图像,而不是其他应用程序,例如照片、文件管理器等

我需要一个直接打开图库应用的解决方案,或者是否可以使用照片应用来选择图像?

1) 从图库中选择

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent,CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

2) onActivityResult 结果代码

try {
    // bimatp factory
    BitmapFactory.Options options = new BitmapFactory.Options();
    // downsizing image as it throws OutOfMemory Exception for larger images
    options.inSampleSize = 2;
    final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);

    descimage.setImageBitmap(bitmap);

    bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(new File(fileUri.getPath())));

    photostatus = 1;
    pbar.setVisibility(View.VISIBLE);

    txtbrowser.setEnabled(false);
    new upload().execute();

} catch (NullPointerException e) {
        e.printStackTrace();
}

【问题讨论】:

    标签: android image gallery


    【解决方案1】:

    此意图允许您从默认图库中选择图像。

     // in onCreate or any event where your want the user to  select a file
               Intent intent = new Intent();
               intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                                    "Select Picture"), SELECT_PICTURE);
    

    用于接收onActivityResult()中选择的图片

     public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_PICTURE) {
                    Uri selectedImageUri = data.getData();
                    selectedImagePath = getPath(selectedImageUri);
                }
            }
        }
    

    我从here得到了解决方案

    【讨论】:

      【解决方案2】:

      您应该知道Gallery no longer exists 在某些运行 Lollipop 的设备上。照片应用程序是替代品,它在处理选择图像的意图时应该没有问题。 Intent.ACTION_GET_CONTENT 通常为recommended for selecting images,如:

      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
      intent.setType("image/*");
      startActivityForResult(intent, ID);
      

      讨论了在安装了图库的设备上打开图库here。基本上每个不同的供应商可能会发布不同的画廊应用程序。

      通过使用PackageManager.queryIntentActivities() API 迭代用户设备上的所有可用包,您可以显式启动您需要的。

      【讨论】:

        猜你喜欢
        • 2015-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-13
        • 1970-01-01
        相关资源
        最近更新 更多