如何从意图中获取完整尺寸和分辨率的照片(Android 7)

时间:2017-06-19 13:35:11

标签: android android-intent android-camera-intent android-7.1-nougat

我第一次尝试拍摄有意图的全尺寸照片时已经有一段时间了。我尝试缩放缩略图,使用URI,我也尝试了FileProvider,但它似乎没有效果。 我确定我将所有permissions放在AndroidManifest.xml

这是我的代码:

static final int REQUEST_IMAGE_CAPTURE = 1;
    public void openCamera(View view)
    {        
        photoName = "";

        if (ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
        {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA))
            {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 0x5);
            }
        }
        else
        {
            cameraCapture();
        }
    }

private void cameraCapture()
    {
        File image = null;
        try
        {
            File filePath = new File(getFilesDir(), "myfolder");
            image = new File(filePath + File.separator + "myimage.jpg");
            File storageDir = Environment.getExternalStorageDirectory();

            if(!image.exists())image.createNewFile();
            currentImagePath = image.getAbsolutePath();
        }
        catch(IOException e)
        {}

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null)
        {
            if (image != null)
            {
                Uri imageUri = FileProvider.getUriForFile(this, "com.touchmultimedia.testpermessi.provider", image);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == REQUEST_IMAGE_CAPTURE)
        {
            if (resultCode == RESULT_OK)
            {
                File photoFile = new File(photoUri.getPath());
                if (photoFile != null && photoFile.exists())
                {
                    Bitmap b = BitmapFactory.decodeFile(photoUri.getPath());
                    imgView.setImageBitmap(b);
                    imgView.setVisibility(View.VISIBLE);

                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                    photoName = "IMG_" + timeStamp + ".jpg";

                    //deleteTemps(photoFile);

                    saved = 0;
                }
            }
        }
    }

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED)
        {
            switch(requestCode)
            {
                //Camera
                case 5:

                    cameraCapture();
                    break;

                //Write External Storage
                case 3:

                    saveImage();
                    break;
            }
        }
    }

AndroidManifest.xml

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.camera"
        android:required="true"/>
    <uses-feature android:name="android.hardware.camera.autofocus" />

我已经阅读了所有其他示例,但没有一个能帮我解决问题。

感谢所有能帮助我的人!

2 个答案:

答案 0 :(得分:0)

如果“myfolder”不存在,您还需要确保“myfolder”。 image.createNewFile()不会自动为您创建“myfolder”。

答案 1 :(得分:0)

以下是我的应用中的代码:

public void startCameraIntent(String fileName) {
    File file = new File(fileName);
    Uri outputFileUri = Uri.fromFile(file);
    Intent intent;
    int currentAPIVersion = android.os.Build.VERSION.SDK_INT;
    String deviceManufacturer = android.os.Build.MANUFACTURER;
    String samsung = "samsung";
    if (currentAPIVersion >= android.os.Build.VERSION_CODES.M && deviceManufacturer.compareTo(samsung) != 0) {
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
    } else {
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    }
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(intent, TAKE_PICTURE_REQUEST_CODE);
}

请注意制造商检查的行。自Android M以来,三星对图像捕获使用不同的意图动作。