Android 拍摄后画质低
Android Low picture quality after shot
我有一个触发图像捕获的按钮:
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);
同样使用文件提供程序选项。
在清单文件中添加提供商描述:
<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是危险权限。
在 class 中,您在其中启动相机 :
声明一个常数以传递相机意图:
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);
希望这对您有所帮助。如果您选择此程序并卡在某个地方,请告诉我。
我有一个触发图像捕获的按钮:
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);
同样使用文件提供程序选项。
在清单文件中添加提供商描述:
<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是危险权限。
在 class 中,您在其中启动相机 :
声明一个常数以传递相机意图:
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);
希望这对您有所帮助。如果您选择此程序并卡在某个地方,请告诉我。