【问题标题】:Android Low picture quality after shotAndroid 拍摄后画质低
【发布时间】:2018-02-13 04:00:30
【问题描述】:

我有一个触发图像捕获的按钮:

private void capturePicture() {
    if(ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Tiến hành gọi Capture Image intent
        startActivityForResult(intent, 100);
    } else ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 200);
}

刚拍完照片,我就看到了截图,而且分辨率很低。它保存在本地存储中,当它显示在应用程序中时,它不是原始质量。

private void loadImageFromStorage(String path, int position, Device device)
{

    try {
        File f=new File(path,device.getSerial()+".jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
        imvDeviceImage.setImageBitmap(b);
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }

}

这里是为了获得许可

 if(ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(intent, 100);
    } else ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 200);

【问题讨论】:

标签: android local-storage android-camera resolution blurry


【解决方案1】:

使用相同的文件提供程序选项。

在清单文件中添加提供者描述:

   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="YOUR_PACKAGE_NAME.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/image_path" />
    </provider>

添加权限:

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

在 res/xml 文件夹下添加一个路径文件(我已经制作了 image_path.xml,在文件提供者的描述中添加)。 (如果 xml 不存在,则创建一个名为 xml 的文件夹。)

    <?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path
        name="captured_image"
        path="Gallery/MyImages/" />
</paths>

为棒棒糖上方的设备添加危险权限请求程序,因为 WRITE_EXTERNAL_STORAGE 是危险权限。

在您启动相机的类中:

声明一个常量以传入相机意图:

private int ACTION_REQUEST_CAMERA = 8765;
  private String CAPTURE_IMAGE_FILE_PROVIDER = "YOUR_PACKAGE.fileprovider";

现在,您可以在相机意图中传递 uri。

    public void launchCamera() {
// check for dangerous permission 
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
            File path = new File(activity.getFilesDir(), "Gallery/MyImages/");
            if (!path.exists()) path.mkdirs();
            File image = new File(path, "image_capture.jpg");
            Uri imageUri = FileProvider.getUriForFile(activity.getApplicationContext(), CAPTURE_IMAGE_FILE_PROVIDER, image);
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            providePermissionForProvider(cameraIntent, imageUri);
            startActivityForResult(cameraIntent, ACTION_REQUEST_CAMERA);
        } else {
            //if permission is not provided
            new PermissionsMarshmallow(activity);
        }
    }

::已编辑:

忘记添加这个功能了:

 private void providePermissionForProvider(Intent intent, Uri uri) {
    List<ResolveInfo> resInfoList = activity.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    for (ResolveInfo resolveInfo : resInfoList) {
        String packageName = resolveInfo.activityInfo.packageName;
        activity.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
}

在 onActivityResult 函数中:

//get the file from the uri path  , it is the same declared in the path file in xml folder. in this case it is Gallery/MyImages/
     File path = new File(activity.getFilesDir(), "Gallery/MyImages/");
                    if (!path.exists()) path.mkdirs();
                    File imageFile = new File(path, "image_capture.jpg");

                    Uri stringUri = Uri.fromFile(imageFile);

                    Bitmap bitmapFromMedia = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), stringUri);

希望这会有所帮助。让我知道您是否选择了此程序并被困在某个地方。

【讨论】:

    猜你喜欢
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    相关资源
    最近更新 更多