我正在制作一个简单的图片上传应用程序,该应用程序允许用户从手机图库上传照片并从手机摄像头捕获到Firebase服务器。
目前,我可以显示和上传我从图库中选择的图像,但是我无法上传从相机中捕获的图像。
以下是我的代码:
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Gallery", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(Upload_Image.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
userChosenTask ="Take Photo";
cameraIntent();
} else if (items[item].equals("Choose from Gallery")) {
userChosenTask ="Choose from Gallery";
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void galleryIntent()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}
private void cameraIntent()
{
/*Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);*/
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
Bitmap bitmap = null;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
String uri = outPutfileUri.toString();
Log.e("uri-:", uri);
Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), outPutfileUri);
Drawable d = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
file = data.getData();
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), file);
} catch (IOException e) {
e.printStackTrace();
}
}
imageView.setImageBitmap(bm);
imageView.setImageURI(file);
}
private void uploadImage()
{
if(file!=null)
{
FirebaseStorage storage=FirebaseStorage.getInstance();
StorageReference reference=storage.getReferenceFromUrl("gs://citycare-b8a9d.appspot.com");
StorageReference imagesRef=reference.child("images/"+editTextName.getText().toString());
UploadTask uploadTask = imagesRef.putFile(file);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
pd.dismiss();
Toast.makeText(Upload_Image.this, "Error : "+e.toString(), Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
pd.dismiss();
Toast.makeText(Upload_Image.this, "Uploading Done!!!", Toast.LENGTH_SHORT).show();
}
});
}
}
}