从相机意图接收低质量图像
Receiving low quality image from camera intent
我正在尝试使用相机将图像上传到 firebase。但是,我得到的图像质量太低(不可读)。我写了下面的代码(没有编译错误)。但是,无法理解为什么它无法生成可读图像。
我尝试改变 bitmap.compress 的质量参数,但图像质量保持不变。但是相同的压缩代码适用于来自图库的图像。
uploadPp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(infoPp.this, android.R.style.Theme_Material_Dialog_NoActionBar_MinWidth);
} else {
builder = new AlertDialog.Builder(infoPp.this);
}
builder.setTitle("Picture Source")
.setMessage("Select source of picture")
.setPositiveButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE_CNIC);
}
}
})
.setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_IMAGE_CAPTURE_CNIC);
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mProgress.setMessage("Uploading.....");
if (requestCode == REQUEST_IMAGE_CAPTURE_CNIC && resultCode == RESULT_OK) {
Bitmap imageBitmap;
try {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
mProgress.show();
encodeBitmapAndSaveToFirebase(imageBitmap,"ProfilePhoto");
}catch(Exception e) {
Uri file = data.getData();
mProgress.show();
encodeBitmapAndSaveToFirebase(file,"ProfilePhoto");
}
}
}
public void encodeBitmapAndSaveToFirebase(Bitmap bitmap,String fileName) { //////////// THISIS FOR CAMERA
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
path= "Verification Records/"+auth.getCurrentUser().getUid()+"/"+fileName+".jpeg";
byte[] data=baos.toByteArray();
StorageReference firememeRef = storage.getReference(path);
UploadTask uploadTask= firememeRef.putBytes(data);
...
}
public void encodeBitmapAndSaveToFirebase(Uri file,String fileName){ /// THIS IS FOR GALLERY
Uri selectedImage = file;
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(
selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 40, stream);
String path= "Verification Records/"+auth.getCurrentUser().getUid()+"/"+fileName+".jpeg";
byte[] data=stream.toByteArray();
StorageReference firememeRef = storage.getReference(path);
UploadTask uploadTask= firememeRef.putBytes(data);
...
}'
data
extra 中提供的图像是 only a thumbnail. To obtain the fullsize image you need to provide a fully qualified file name where the camera app should save the photo. This is described in the documentation,并提供了示例代码。
我正在尝试使用相机将图像上传到 firebase。但是,我得到的图像质量太低(不可读)。我写了下面的代码(没有编译错误)。但是,无法理解为什么它无法生成可读图像。
我尝试改变 bitmap.compress 的质量参数,但图像质量保持不变。但是相同的压缩代码适用于来自图库的图像。
uploadPp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(infoPp.this, android.R.style.Theme_Material_Dialog_NoActionBar_MinWidth);
} else {
builder = new AlertDialog.Builder(infoPp.this);
}
builder.setTitle("Picture Source")
.setMessage("Select source of picture")
.setPositiveButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE_CNIC);
}
}
})
.setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_IMAGE_CAPTURE_CNIC);
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mProgress.setMessage("Uploading.....");
if (requestCode == REQUEST_IMAGE_CAPTURE_CNIC && resultCode == RESULT_OK) {
Bitmap imageBitmap;
try {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
mProgress.show();
encodeBitmapAndSaveToFirebase(imageBitmap,"ProfilePhoto");
}catch(Exception e) {
Uri file = data.getData();
mProgress.show();
encodeBitmapAndSaveToFirebase(file,"ProfilePhoto");
}
}
}
public void encodeBitmapAndSaveToFirebase(Bitmap bitmap,String fileName) { //////////// THISIS FOR CAMERA
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
path= "Verification Records/"+auth.getCurrentUser().getUid()+"/"+fileName+".jpeg";
byte[] data=baos.toByteArray();
StorageReference firememeRef = storage.getReference(path);
UploadTask uploadTask= firememeRef.putBytes(data);
...
}
public void encodeBitmapAndSaveToFirebase(Uri file,String fileName){ /// THIS IS FOR GALLERY
Uri selectedImage = file;
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(
selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 40, stream);
String path= "Verification Records/"+auth.getCurrentUser().getUid()+"/"+fileName+".jpeg";
byte[] data=stream.toByteArray();
StorageReference firememeRef = storage.getReference(path);
UploadTask uploadTask= firememeRef.putBytes(data);
...
}'
data
extra 中提供的图像是 only a thumbnail. To obtain the fullsize image you need to provide a fully qualified file name where the camera app should save the photo. This is described in the documentation,并提供了示例代码。