2016-11-25 55 views
0

我创建了一个简单的应用程序,用户必须将图像从图库上传到Firebase。第一次裁剪图像,但现在如何保存裁剪后的图像。如何将作物图像保存在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。

回答

0

试试这个,

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

uploadFile你必须写你的火力调用来更新像您的数据。

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; 
}