Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从照相机中挑选图像是可行的,但不是从Gallary[视频演示]_Android - Fatal编程技术网

Android 从照相机中挑选图像是可行的,但不是从Gallary[视频演示]

Android 从照相机中挑选图像是可行的,但不是从Gallary[视频演示],android,Android,我已经跟随了一些关于如何从照相机或Gallary检索图像的例子。照相机的部分能用,但勇敢的部分不行。代码对我来说似乎很难理解,所以我不知道到底要注意什么 我在清单中还具有所需的权限 以下是问题的视频: 图像选择器(视图V)、意图/选择器、文件、URI public void ImagePicker(View v) { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

我已经跟随了一些关于如何从照相机或Gallary检索图像的例子。照相机的部分能用,但勇敢的部分不行。代码对我来说似乎很难理解,所以我不知道到底要注意什么

我在清单中还具有所需的权限

以下是问题的视频:

图像选择器(视图V)、意图/选择器、文件、URI

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

使用下面的代码从库中拾取图像

   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()中:


这不符合我的代码。你看到我的代码了吗?你在哪里传递画廊的意图行动。。。?请给出我的答案,我正在将操作传递给意图。我在这里有:
final intent imageChooser=new intent();setType(“image/*”);imageChooser.setAction(Intent.ACTION\u GET\u CONTENT)Intent-chooserentent=Intent.createChooser(图像选择器,“选择源”)那么问题出在哪里…你对gallery的结果满意吗?我为你制作了一个视频,向你展示了正在发生的事情(:
   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);
 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 ...");
      }
}