如何在Firebase中保存裁剪图像

时间:2016-11-25 07:42:35

标签: android

我创建了一个简单的应用程序,用户必须将图片从图库上传到Firebase。图像首先裁剪但现在如何保存此裁剪图像。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    su = (TextView) findViewById(R.id.sub);
    name = (EditText) findViewById(R.id.namee);
    register = (Button) findViewById(R.id.sigupp);
    emaill = (EditText) findViewById(R.id.emaitext);
    password = (EditText) findViewById(R.id.passtext);
    mAuth = FirebaseAuth.getInstance();
    mStorageImage=      FirebaseStorage.getInstance().getReference()
                        .child("profile_images");
                mProgres = new ProgressDialog(this);

     photo=(ImageView)findViewById(R.id.welcom);
    mReference =       FirebaseDatabase.getInstance().getReference().child("user");
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {

                // User is signed in
               // Log.d(TAG, "onAuthStateChanged:signed_in:" + 
                   user.getUid());
            } else {
                // User is signed out
               // Log.d(TAG, "onAuthStateChanged:signed_out");
               // Intent i=new Intent(MainActivity.this,Home.class);

                //startActivity(i);

            }
            // ...

这是裁剪图像的代码。

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


            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。

1 个答案:

答案 0 :(得分:0)

试试这个,

File sendPic = new File(Utils.getRealPathFromURI(selectedImageURI, context));
                uploadFile(Utils.convert_Bitmap_to_Base64(sendPic, null));

uploadFile中,您必须编写firebase调用以更新数据,如。

private void uploadFile(String encodedImage) {
    if (encodedImage == null)
        return;

    Utils.showProgressDialog((Activity) context);
    HashMap<String, Object> file_map = new Firebase_Adapter().saveImage(key, encodedImage);
    if (file_map != null) {
            FirebaseSingleton.getInstance().getFirebase().updateChildren(file_map, new Firebase.CompletionListener() {
                @Override
                public void onComplete(FirebaseError firebaseError, Firebase firebase) {
                    Utils.hideProgressDialog();
                    if (firebaseError == null) {
                        Utils.setLog("Image file uploaded successfully");
                    }
                    else
                        Utils.setLog("firebaseError : " + firebaseError.getDetails());
                }
            });
    }
}

saveImage中你写得像,

public HashMap<String, Object> saveImage(String TASK_KEY, String encodedImage) {
    if (encodedImage != null && encodedImage.length() > 0 && TASK_FILE_KEY != null) {

        HashMap<String, Object> file_map = new HashMap<>();
        file_map.put(FirebaseUtils.FILES + "/" + TASK_FILE_KEY + "/image", encodedImage);
        file_map.put(FirebaseUtils.FILES + "/" + TASK_FILE_KEY + "/owner", FirebaseUtils.getUID());
        file_map.put(FirebaseUtils.FILES + "/" + TASK_FILE_KEY + "/created_at", Utils.getFirebaseCurrentTimestamp());

        Utils.setLog("file_map : " + file_map.toString());
        return file_map;
    } else
        return null;
}