拍摄后Android低画质 [英] Android Low picture quality after shot

查看:107
本文介绍了拍摄后Android低画质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a button that triggers an image capture :

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

拍完照片后,我立即看到了快照,分辨率很低.它保存在本地存储中,并且在应用程序中显示时,它不是原始质量.

Right after I take the shot, I see the capture and the resolution is very low. It is saved in the local storage and when it is displayed in the app it's not in its original quality.

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

推荐答案

使用文件提供程序选项.

Use File provider option for the same.

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

In the manifest file add the provider description :

   <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的文件夹.)

add a path file( i have made image_path.xml, added in the description of file provider) under res/xml folder. (if xml is not there, make one folder named 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是危险许可.

Add dangerous permission request procedure for devices above lollipop, since WRITE_EXTERNAL_STORAGE is a dangerous permission.

在班级中,您启动相机:

In the class, in which you initiate Camera :

声明要传递摄像机意图的内容:

Declare a contant to pass in camera intent:

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

现在,您可以改为在相机意图中传递uri.

Now, you can pass the uri instead in the camera intent.

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

:: Edited:

::Edited :

忘记添加此功能:

 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函数中:

In the onActivityResult function :

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

希望这会有所帮助.让我知道您是否选择此程序,并被卡在某处.

Hope this helps. let me know if you opt for this procedure and are stuck somewhere.

这篇关于拍摄后Android低画质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆