从相机中选取图像,但不是从Gallary [视频演示]

时间:2016-04-27 08:43:47

标签: android

我已经关注了如何从Camera或Gallary中检索图像的一些例子。相机部件可以正常工作,但是可以使用相应的部件。代码对我来说似乎非常难以理解,所以我不知道究竟要照顾什么。

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

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

ImagePicker(View 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();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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