我的应用程序中有三个按钮。一种用于相机,第二种用于画廊,第三种用于上传。我将上传方法从网络服务器更改为Google Firebase。目前,我只能通过相机按钮上传。如果我尝试从图库按钮上载,它将开始在一个临时文件夹中搜索图像,该临时文件夹将在相机拍摄图像时将其保存。
从图库中获取图片:
private void ImageSelection()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, IMAGE_REQUEST);
}
关于活动方法:
else if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK && data != null)
{
Uri FilePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), FilePath);
mImageView.setImageBitmap(bitmap);
mImageView.setVisibility(View.VISIBLE);
mEditText.setVisibility(View.VISIBLE);
staticSpinner.setVisibility(View.VISIBLE);
if (TextUtils.isEmpty(mEditText.getText())){
mEditText.setError("Privalomas laukas");
}
} catch (IOException e) {
e.printStackTrace();
}
}
上传方法:
private void UploadImage() {
if(photoFile != null)
{
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
StorageReference ref = storageReference.child("images/"+ staticSpinner.getSelectedItem().toString().trim()+"_"+ mEditText.getText().toString());
ref.putFile(Uri.fromFile(photoFile))
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
mImageView.setImageResource(0);
mImageView.setVisibility(View.GONE);
mEditText.setText("");
mEditText.setVisibility(View.GONE);
staticSpinner.setVisibility(View.GONE);
mCapture.setVisibility(View.VISIBLE);
mChoose.setVisibility(View.VISIBLE);
//mUpload.setVisibility(View.GONE);
photoFile = new File(String.valueOf(photoFile));
photoFile.delete();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
mImageView.setImageResource(0);
mImageView.setVisibility(View.GONE);
mEditText.setText("");
mEditText.setVisibility(View.GONE);
staticSpinner.setVisibility(View.GONE);
mCapture.setVisibility(View.VISIBLE);
mChoose.setVisibility(View.VISIBLE);
//mUpload.setVisibility(View.GONE);
photoFile = new File(String.valueOf(photoFile));
photoFile.delete();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded "+(int)progress+"%");
}
});
}
}
答案 0 :(得分:0)
添加了一个else if语句。愚蠢的我