【发布时间】:2020-06-01 18:14:13
【问题描述】:
我不知道我是否做错了什么,也许我根本没有使用图像,或者我以错误的方式压缩图像,因为当我试图将它发送到服务器时,它回复我说大小超过 10 MB我的手机拍照 jpg 大约 7-9 MB(在 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);*/
}
}
}
【问题讨论】:
标签: java android android-studio photo image-compression