如何将位图保存到Firebase [英] How to save bitmap to Firebase

查看:111
本文介绍了如何将位图保存到Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个裁剪图像的简单应用程序.现在,我想将此图像保存到Fire基地.

I created a simple application which crop the image . Now I want to save this image to the Fire base .

photo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Intent imageDownload = new 
Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      Intent imageDownload=new Intent();
      imageDownload.setAction(Intent.ACTION_GET_CONTENT);
      imageDownload.setType("image/*");
      imageDownload.putExtra("crop", "true");
      imageDownload.putExtra("aspectX", 1);
      imageDownload.putExtra("aspectY", 1);
      imageDownload.putExtra("outputX", 200);
      imageDownload.putExtra("outputY", 200);
      imageDownload.putExtra("return-data", true);
      startActivityForResult(imageDownload, GALLERY_REQUEST_CODE);


        }
    });
 }
  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
  data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK && 
   data != null) {
        Bundle extras = data.getExtras();
        image = extras.getParcelable("data");
        photo.setImageBitmap(image);

   }






}

如何将此图像保存到Firebase.我尝试了许多教程,但未能成功.请使用简单的代码进行验证.

How to save this image to the Firebase . I tried many tutorial but could not succeed . Please verify with simple code .

推荐答案

,您必须首先将Firebase Storage的依赖项添加到build.gradle文件中:

you must first add the dependencies for Firebase Storage to your build.gradle file:

compile 'com.google.firebase:firebase-storage:10.0.1'
compile 'com.google.firebase:firebase-auth:10.0.1'

然后创建一个FirebaseStorage实例:

then create an instance of FirebaseStorage:

FirebaseStorage storage = FirebaseStorage.getInstance();

要将文件上传到Firebase Storage,首先要创建对文件完整路径的引用,包括文件名.

To upload a file to Firebase Storage, you first create a reference to the full path of the file, including the file name.

// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl("gs://<your-bucket-name>");

// Create a reference to "mountains.jpg"
StorageReference mountainsRef = storageRef.child("mountains.jpg");

// Create a reference to 'images/mountains.jpg'
StorageReference mountainImagesRef = storageRef.child("images/mountains.jpg");

// While the file names are the same, the references point to different files
mountainsRef.getName().equals(mountainImagesRef.getName());    // true
mountainsRef.getPath().equals(mountainImagesRef.getPath());    // false

一旦创建了适当的引用,就可以调用putBytes(),putFile()或putStream()方法将文件上传到Firebase Storage.

Once you've created an appropriate reference, you then call the putBytes(), putFile(), or putStream() method to upload the file to Firebase Storage.

putBytes()方法是将文件上传到Firebase Storage的最简单方法. putBytes()取一个byte []并返回一个UploadTask,可用于管理和监视上传状态.

The putBytes() method is the simplest way to upload a file to Firebase Storage. putBytes() takes a byte[] and returns an UploadTask that you can use to manage and monitor the status of the upload.

// Get the data from an ImageView as bytes
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();

UploadTask uploadTask = mountainsRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle unsuccessful uploads
    }
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
        Uri downloadUrl = taskSnapshot.getDownloadUrl();
    }
});

这篇关于如何将位图保存到Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆