拍摄后Android低画质

时间:2018-02-13 04:00:31

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

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

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

After shooting In the app

1 个答案:

答案 0 :(得分: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);

希望这会有所帮助。如果你选择这个程序并且卡在某个地方,请告诉我。