2017-01-10 62 views
0

我沒有編碼經驗,我只是從android studio中的教程創建應用程序。我在創建應用程序方面走了很長的路。我已經實現了從devive圖庫中選擇圖片的代碼,現在我想知道如何將該圖片設置爲當前用戶的Firebase PhotoURL?Android studio Firebase如何設置用戶從設備存儲中作爲用戶選擇的圖片。PhotoURL

+2

把它上傳到FirebaseStorage和回調返回包含下載網址https://firebase.google.com/docs/storage/ – Linxy

回答

0

我已經實施了,而我正在嘗試firebase。希望這會做你的工作。

getImageFromMobile設置爲我用來設置圖像的onClick方法ImageButton。

public void getImageFromMobile(View view) { 
     if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ 
      ActivityCompat.requestPermissions(this,new String[]{ 
        android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1); 
     } 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.setType("image/"); 
     startActivityForResult(intent , galleryRequestCode); 
    } 



private void postDataToFirebase() { 
      mProgressDialog.setMessage("Posting the Blog to Firebase"); 
      mProgressDialog.setCancelable(false); 

      final String titleValue = mPostTitle.getText().toString(); 
      final String description = mPostDescription.getText().toString(); 
      if((!TextUtils.isEmpty(titleValue))&& (!TextUtils.isEmpty(description)) && bitmap != null) 
      { 
       mProgressDialog.show(); 
       StorageReference filePath = mStorage.child("Blog_Images").child(imagePathName); 
       ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 20, bytes); 
       String path = MediaStore.Images.Media.insertImage(PostActivity.this.getContentResolver(), bitmap, imagePathName, null); 
       Uri uri = Uri.parse(path); 
       filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
        @Override 
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 

         Uri downloadUrl = taskSnapshot.getDownloadUrl(); 

         DatabaseReference newPost = mDatabaseReference.push(); 
         newPost.child("Title").setValue(titleValue); 
         newPost.child("Desc").setValue(description); 
         newPost.child("imageUrl").setValue(downloadUrl.toString()); 
         Toast.makeText(PostActivity.this, "Data Posted Successfully to Firebase server", Toast.LENGTH_LONG).show(); 
         mProgressDialog.dismiss(); 
         Intent intent = new Intent(PostActivity.this, MainActivity.class); 
         startActivity(intent); 
        } 
       }); 

      } 

     } 




@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if(requestCode == galleryRequestCode && resultCode == RESULT_OK){ 

      Uri imageUri = data.getData(); 
      imagePathName = imageUri.getLastPathSegment(); 
      Log.i("ImagePathName",imagePathName); 
      Toast.makeText(this, "ImagePathNameto be Checked" + imagePathName, Toast.LENGTH_SHORT).show(); 
      try { 
       bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri); 
       imageButton.setImageBitmap(bitmap); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
    } 
+0

不要忘記設置 私人INT galleryRequestCode = 1任務; – AndroidBeginner

相關問題