【问题标题】:How to take a full size and resolution photo from intent (Android 7)如何从意图中获取完整尺寸和分辨率的照片(Android 7)
【发布时间】:2017-06-19 13:35:11
【问题描述】:

我第一次尝试带着意图拍摄全尺寸照片已经有一段时间了。我尝试缩放缩略图,使用 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" />

我已阅读所有其他示例,但没有一个对我的问题有帮助。

感谢所有可以帮助我的人!

【问题讨论】:

  • 详细解释什么是“它似乎不起作用”。 Here is a sample app 演示使用 ACTION_IMAGE_CAPTUREFileProvider。但请记住,并非所有支持 ACTION_IMAGE_CAPTURE 的 Android 相机应用程序都可以使用 content Uri,就像您从 FileProvider 获得的一样。
  • @CommonsWare 随着FileProvider 应用程序崩溃,可能是因为我犯了一些错误,我遵循了 Android 指南,但它们没有帮助

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


【解决方案1】:

如果“myfolder”不存在,您需要确保它也被创建。 image.createNewFile() 不会自动为您创建“我的文件夹”。

【讨论】:

  • 我知道,但我认为问题不存在,因为在调试过程中我可以获取文件夹 URI,所以我认为它存在
【解决方案2】:

这是我的应用程序的工作代码:

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 以来,三星使用不同的意图操作进行图像捕获。

【讨论】:

    猜你喜欢
    • 2014-03-07
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多