我的代码有问题,我必须将图像上传到firebase存储,我需要图像来自图库和相机,图库中的图像都可以,但来自相机的图像是给出问题,在imageView中加载并发送到数据库的图像是黑色的。有谁知道如何解决这个问题,或者你知道任何其他加载图像的方法吗?
//Funçao de escolha das opcoes de upload---------------------------------------------------------
private void jeitoImagem() {
final CharSequence[] jeito = {"Camera", "Galeria", "Cancelar"};
//Alert Dialog com as opçoes de escolha-----------------------------------------------------
AlertDialog.Builder b = new AlertDialog.Builder(CadastroEmpresaActivity.this, R.style.Theme_AppCompat_Light_Dialog_Alert);
b.setTitle("Adicionar imagem");
b.setItems(jeito, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if(jeito[i].equals("Camera")){
abrirCamera();
} else if(jeito[i].equals("Galeria")){
abrirGaleria();
} else if(jeito[i].equals("Cancelar")){
dialogInterface.dismiss();
}
}
});
b.create();
b.show();
}
//Funçao de upar imagem da galeria--------------------------------------------------------------
public void abrirGaleria() {
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivityForResult(i,GALERIA);
}
//Funçao de upar imagem da camera---------------------------------------------------------------
public void abrirCamera() {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//passando o data pra variavel uri----------------------------------------------------------
uri = data.getData();
//Verificando a opçao que foi selecionada---------------------------------------------------
来自画廊的
if(requestCode == GALERIA && resultCode == RESULT_OK && uri != null){
//Chamando instancia do Storage---------------------------------------------------------
storageReference1 = FirebaseStorage.getInstance().getReference();
try {
//Setando imagem no imageView-------------------------------------------------------
Bitmap b = MediaStore.Images.Media.getBitmap(CadastroEmpresaActivity.this.getContentResolver(), uri);
imagem.setImageBitmap(b);
//Chamando o Storage pra salvar a imagem--------------------------------------------
StorageReference ref = storageReference1.child("Usuario" + System.currentTimeMillis() +".MiBuy");
ref.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Passando url da imagem pra variavel---------------------------------------
urlImage = taskSnapshot.getDownloadUrl().toString();
//Alert Dialog de aviso do upload da imagem---------------------------------
AlertDialog.Builder b = new AlertDialog.Builder(CadastroEmpresaActivity.this, R.style.Theme_AppCompat_Light_Dialog_Alert);
b.getContext();
b.setTitle(R.string.upload_imagem);
b.setMessage(R.string.texto_upload_imagem);
b.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
b.create();
b.show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
//Verificando a opçao que foi selecionada---------------------------------------------------
来自相机的
if (requestCode == CAMERA && resultCode == RESULT_OK){
//Chamando instancia do Storage---------------------------------------------------------
storageReference2 = FirebaseStorage.getInstance().getReference();
//Caminho da URL------------------------------------------------------------------------
StorageReference mref = storageReference2.child("Usuario"+ System.currentTimeMillis()+"MiBuy");
//Liberando cache nas imagens-----------------------------------------------------------
imagem.setDrawingCacheEnabled(true);
imagem.buildDrawingCache();
//Criando bitmap------------------------------------------------------------------------
Bitmap bitmap = imagem.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] dd = baos.toByteArray();
//Upando a imagem pro firebase----------------------------------------------------------
UploadTask uploadTask = mref.putBytes(dd);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
//Alert Dialog de aviso de erro no upload da imagem-----------------------------
AlertDialog.Builder b = new AlertDialog.Builder(CadastroEmpresaActivity.this, R.style.Theme_AppCompat_Light_Dialog_Alert);
b.getContext();
b.setTitle(R.string.erro_upload_imagem);
b.setMessage(R.string.texto_erro_upload_imagem);
b.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
b.create();
b.show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Alert Dialog de aviso do upload da imagem-------------------------------------
AlertDialog.Builder b = new AlertDialog.Builder(CadastroEmpresaActivity.this, R.style.Theme_AppCompat_Light_Dialog_Alert);
b.getContext();
b.setTitle(R.string.upload_imagem);
b.setMessage(R.string.texto_upload_imagem);
b.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
b.create();
b.show();
//Passando url da imagem pra variavel-------------------------------------------
Uri ddownload = taskSnapshot.getDownloadUrl();
urlImage = taskSnapshot.getDownloadUrl().toString();
//Colocando imagem no imageView
Picasso.with(CadastroEmpresaActivity.this).load(ddownload).into(imagem);
}
});
}
}
答案 0 :(得分:0)
您用于启动相机应用程序的意图仅返回thumbnail image,您可以从返回的数据中获取该文件:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
...
mImageView.setImageBitmap(imageBitmap);
}
}
如果要获取完整大小的图像,则必须创建一个文件来保存图像,为文件创建URI,并将URI作为额外添加到intent中。这是described in the documentation。