【问题标题】:Picking a image from Camera works, but not from the Gallary [VIDEO DEMO]从相机作品中选择图像,但不是从图库中选择图像 [视频演示]
【发布时间】:2016-08-21 10:42:08
【问题描述】:

我遵循了一些关于如何从相机或图库中检索图像的示例。相机部分工作,但画廊部分没有。代码对我来说似乎很难理解,所以我不知道具体要注意什么。

我的清单中也有所需的权限。

这是问题的视频:https://www.youtube.com/watch?v=OOoY1y4W86w

ImagePicker(视图 V)、意图/选择器、文件、URIS

public void ImagePicker(View v) {

    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {

            final File rootdir = new File(Environment.getExternalStorageDirectory() + File.separator + "TravelDiary" + File.separator);
            rootdir.mkdirs();
            final String filename = "img_" + System.currentTimeMillis() + ".jpg";
            final File sdImageMainDirecotry = new File(rootdir, filename);
            outputFileUri = Uri.fromFile(sdImageMainDirecotry);
            Log.d("TAG", "IM HERE 1");

            //camera
            final List<Intent> cameraIntents = new ArrayList<>();
            final Intent CameraCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            final PackageManager packageManager = getPackageManager();
            final List<ResolveInfo> listcam = packageManager.queryIntentActivities(CameraCaptureIntent, 0);
            Log.d("TAG", "IM HERE 2");


            for (ResolveInfo res : listcam) {

                final String packageName = res.activityInfo.packageName;
                final Intent intent = new Intent(CameraCaptureIntent);
                intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                intent.setPackage(packageName);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                cameraIntents.add(intent);
                Log.d("TAG", "IM HERE 3");
            }


            //Gallary
            final Intent imageChooser = new Intent();
            imageChooser.setType("image/*");
            imageChooser.setAction(Intent.ACTION_GET_CONTENT);


            // Chooser of filesystem options.
            final Intent chooserIntent = Intent.createChooser(imageChooser, "Select Source");

            // Add the camera options.

            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
            startActivityForResult(chooserIntent, SELECT_FROM_GALLARY);


        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
    } else {
        Toast.makeText(this, "External storage not available", Toast.LENGTH_SHORT).show();
    }
}

onActivityResult():

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_FROM_GALLARY) {
            final boolean isCamera;
            if (data == null) {
                isCamera = true;
            } else {
                final String action = data.getAction();
                if (action == null) {
                    isCamera = false;
                } else {
                    isCamera = action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }

            Uri selectedImageUri;
            if (isCamera) {

                selectedImageUri = outputFileUri;
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                final Bitmap bitmap = BitmapFactory.decodeFile(selectedImageUri.getPath(), options);
                Drawable drawable = new BitmapDrawable(getResources(), bitmap);
                pic.setBackground(drawable);

            } else {
                selectedImageUri = data == null ? null : data.getData();
                Log.d("ImageURI", selectedImageUri.getLastPathSegment());

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                try {

                    InputStream input = getContentResolver().openInputStream(selectedImageUri);
                    final Bitmap bitmap = BitmapFactory.decodeStream(input, null, options);

                    Drawable drawable = new BitmapDrawable(getResources(), bitmap);
                    pic.setBackground(drawable);


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

【问题讨论】:

    标签: android


    【解决方案1】:

    使用以下代码从图库中选择图片。

       Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
       intent.setType("image/*");
       //Here PICK_FROM_GALLERY is a requestCode
       startActivityForResult(intent, PICK_FROM_GALLERY);
    

    在 onActivityResult() 中:

     if (resultCode == Activity.RESULT_OK && requestCode == PICK_FROM_GALLERY) {
          if (data.getData() != null) {
               mImageUri = data.getData();
          } else {
              //showing toast when unable to capture the image
               Debug.toastValid(context, "Unable to upload Image Please Try again ...");
          }
    }
    

    【讨论】:

    • 这不适用于我拥有的代码。你看到我的代码了吗?
    • 你在哪里传递画廊的意图行动......?请回答我的答案,我正在将行动传递给意图。
    • 我在这里:final Intent imageChooser = new Intent(); imageChooser.setType("image/*"); imageChooser.setAction(Intent.ACTION_GET_CONTENT); 然后我做Intent chooserIntent = Intent.createChooser(imageChooser, "Select Source");
    • 那有什么问题...你的画廊的结果好吗?
    • 我为你制作了一个视频,向你展示发生了什么(:youtube.com/watch?v=OOoY1y4W86w
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多