【发布时间】:2020-06-25 21:36:31
【问题描述】:
在我的 Android 应用程序中,我使用相机意图单击照片并将其上传到我的服务器进行处理。但是,要求图像的分辨率应大于 500X500。
如今,默认相机分辨率要高得多,因此所需的分辨率不是问题。但是,在大多数情况下,分辨率要高得多,图像大小达到 3 MB 左右。因此,将图像上传到服务器需要更长的时间,尤其是在互联网连接较差的地区。
我想在不降低分辨率的情况下减小图像的大小,最好减小到 100 KB 左右。
我通过意图捕获图像的代码是:
imageHolder = (ImageView)findViewById(R.id.cPhoto78);
imageHolder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File outputFile1 = new File(getExternalStorageDirectory(), "/APDCLSBM/APDCL1.jpg");
outputFile1.delete();// Deletes the existing photo
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Toast.makeText(getBaseContext(),"Error in clicking picture",
Toast.LENGTH_SHORT).show();
finish();
}
Intent a = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (photoFile != null) {
Uri outputFileUri = FileProvider.getUriForFile(photoCamera.this,
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
a.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(a, 1);
}
}
});
以下函数创建图像文件和路径:
private File createImageFile() throws IOException {
String imageFileName = "APDCL1";
File storageDir = new File(
getExternalStorageDirectory() + IMAGE_DIRECTORY);
File image = new File(storageDir, imageFileName+".jpg");
currentPhotoPath = image.getAbsolutePath();
Log.d("path1",currentPhotoPath);
return image;
}
请推荐一个方法
【问题讨论】:
-
你的意思是“减小文件大小”吗?
-
The following function creates the image file and path:。不,幸运的是它没有。它只创建一个 File 对象。你知道一条路。但是还没有图像文件。这留给相机应用程序。 -
拍摄照片后,将文件加载到位图中(如果可能的话)。然后使用较低的质量百分比再次压缩为 jpg 文件。
-
"Error in clicking picture"你可以做得更好。将ex.getMessage()添加到字符串中。 -
finish();。那太苛刻了。替换为return;。