我不知道我做错了什么可能是我根本没有使用图像或以错误的方式压缩了图像,因为当试图将其发送到服务器时,它会回应我说,当我的图像大小超过10 MB时,手机拍摄约7-9 MB的jpg图片(在Edit.java中,我有一条评论,说我以前使用过缩略图,但是需要更改它,因为当我试图在桌面上查看缩略图时,缩略图的质量很差)
这是我的代码:
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"></uses-feature>
<provider
android:authorities="cam.com.example.fileprovider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path"/>
</provider>
file_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
path="/"/>
<external-files-path
name="external_files"
path="/"/>
<cache-path
name="cache"
path="/"/>
<external-cache-path
name="external_cache"
path="/"/>
<files-path
name="files"
path="/"/>
</paths>
Edit.java
btn_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
REQUEST_IMAGE_CAPTURE = 1;
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
File imageFile = null;
try{
imageFile=getImageFile();
}catch (IOException e){
e.printStackTrace();
}
if(imageFile!=null){
Uri imageUri = FileProvider.getUriForFile(Edit.this,"cam.com.example.fileprovider",imageFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
} catch (Exception e) {
Toasty.warning(getApplicationContext(), IC, Toast.LENGTH_SHORT, true).show();
}
}
});
public File getImageFile() throws IOException{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageName = "jpg_"+timeStamp+"_";
File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File imageFile = File.createTempFile(imageName,".jpg",storageDir);
currentImagePath = imageFile.getAbsolutePath();
return imageFile;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
if (imagenString == null) {
File imgFile = new File(currentImagePath);
String path = imgFile.getAbsolutePath();
Bitmap mybitmap = BitmapFactory.decodeFile(path);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
mybitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
imagenString = Base64.encodeToString(byteArray, Base64.DEFAULT);
/* **Before I was doing this, but the thumbnail has such a bad quality so needed to change it**
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
imagenString = Base64.encodeToString(byteArray, Base64.DEFAULT);*/
}
}
}
您正在使用用户选择的相机应用来拍照。通常,该图像将另存为JPEG图像。 JPEG图像是压缩的图像格式,已针对照片等“真实”图像进行了优化。
然后您尝试将其全部读取到内存中。这不是一个好计划,因为您可能没有足够的内存来存储完整尺寸的照片。
然后您尝试获取结果Bitmap
并将其编码为PNG。 PNG是一种压缩图像格式,但是它是为图标和其他图稿设计的。在PNG中,照片几乎总是会比JPEG占用更多的空间。更糟糕的是,您试图将其编码为PNG到内存中-再次,您可能没有足够的内存来执行此操作。
然后您尝试将编码的PNG转换为base-64。这将比编码的PNG占用更多的空间,并且再次,您可能没有足够的存储空间。
我希望您的应用经常因OutOfMemoryError
而崩溃。
最好的解决方案是摆脱大多数,而直接从磁盘上载JPEG。不要将其加载到内存中,不要将其转换为PNG,也不要将其转换为base-64。
这是因为您要将图像转换为Base64编码的字符串,这使数据的大小变大。
在下面的代码中,您使用100进行压缩,100是最小的压缩和高质量。
mybitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
您可以尝试压缩更多的图像,但是会稍微降低质量:
mybitmap.compress(Bitmap.CompressFormat.PNG, 70, byteArrayOutputStream);