在我的应用程序中使用相机捕获的图片保存在外部存储中的文件夹中,当我检查该文件夹时,发现它的质量与图库中的同一张图片相比质量较低。使用FileOutputStream将图片保存到还有其他方法可以保存图像而不损失质量吗?下面是我的代码:
将捕获的图像保存到文件夹的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==2&&resultCode==RESULT_OK)
{
File camerafile=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/CameraTestFile");
if (!camerafile.exists())
{
camerafile.mkdir();
Toast.makeText(getApplicationContext(),"Folder created",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Folder already exists ",Toast.LENGTH_SHORT).show();
}
Bitmap bitmap;
bitmap= (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream);
try {
FileOutputStream fileOutputStream=new FileOutputStream(camerafile+"/camera3.png");
fileOutputStream.write(byteArrayOutputStream.toByteArray());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
请参阅我对https://stackoverflow.com/a/50929913/9882512>
的回答您可以使用鳕鱼。只需更改文件的路径即可。它将直接将文件保存到所需位置,并且无需复制文件
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
String pictureName = getPictureName();
imagefile = new File(pictureDirectory, pictureName);
picUri = Uri.fromFile(imagefile);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);