将相机图像上传到 Firebase
Posted
技术标签:
【中文标题】将相机图像上传到 Firebase【英文标题】:upload camera image to firebase 【发布时间】:2018-03-09 09:15:43 【问题描述】:我需要将图像从相机和图库存储到 Firebase。在我从画廊上传的项目中,但是当我尝试从相机上传时,什么也没有发生。单击按钮时,您可以在图库或相机之间进行选择。当拍照或从图库中选择图像时,在 imageview 我得到图片。然后我点击保存,如果图片来自画廊,则保存到存储中,并在数据库中创建孩子,但如果图片来自相机,它将无法工作。这个问题有解决办法吗?
MainActivity.java
public class MainActivity extends AppCompatActivity
private Uri mImageUri = null;
private Button btn,mSubmitBtn;
private ImageView imageview;
private int GALLERY = 1, CAMERA = 2;
private StorageReference mStorage;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
mSubmitBtn = (Button)findViewById(R.id.submit);
imageview = (ImageView) findViewById(R.id.iv);
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Upload");
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showPictureDialog();
);
mSubmitBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
startPosting();
);
private void showPictureDialog()
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems =
"Select photo from gallery",
"Capture photo from camera" ;
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
switch (which)
case 0:
choosePhotoFromGallary();
break;
case 1:
takePhotoFromCamera();
break;
);
pictureDialog.show();
public void choosePhotoFromGallary()
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
private void takePhotoFromCamera()
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
////////////////////////////
private void startPosting()
if (mImageUri !=null)
StorageReference filepath = mStorage.child("Images").child(mImageUri.getLastPathSegment());
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>()
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
Uri downloadUrl = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();//push kreira uniq random id
newPost.child("image").setValue(downloadUrl.toString());
);
////////////////////////
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED)
return;
if (requestCode == GALLERY)
if (data != null)
mImageUri = data.getData();
imageview.setImageURI(mImageUri);
else if (requestCode == CAMERA)
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageview.setImageBitmap(thumbnail);
imageview.setImageURI(mImageUri);
【问题讨论】:
这种情况发生在 naugat 及以上的 android 版本上吗? 是的,在牛轧糖版本上 【参考方案1】:我遇到了同样的问题,这主要发生在 Kitkat 和更低版本上。当点击图像而不是直接获取位图文件时,请尝试以下方式,
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
catch (IOException e)
e.printStackTrace();
imageview.setImageBitmap(thumbnail);
同样在清单文件中确保您已经声明了以下权限以及 READ_EXTERNAL
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
出现此问题的原因是 Android 4.4 (KitKat) 更改了对 SD 卡的权限处理方式。因此,最好的做法是在 SD 卡上创建自己的目录来处理您的应用程序需要写入的所有文件。这也适用于所有更高版本。
希望这会有所帮助。
【讨论】:
以上是关于将相机图像上传到 Firebase的主要内容,如果未能解决你的问题,请参考以下文章
Flutter AZURE BLOB IMAGE UPLOAD - 如何将使用移动相机拍摄的图像上传到 azure blob 存储
从画廊或相机上传图像到rest api(错误:状态:错误,消息:用户不存在!,数据:空)