2017-06-13 118 views
0

我想将图片上传到firebase存储,但我没有看到进度对话框和吐司消息。Firebase图片上传 - Android

调试断点甚至不在这些行停止。

请帮忙。

ib_profileImage.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent_modifyImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivity(intent_modifyImage); 
     } 
    }); 
} 

//Image Capture Activity Result 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){ 
     progressDialog.setMessage("Uploading Image..."); 
     progressDialog.show(); 
     Uri uri = data.getData(); 
     StorageReference filepath = storageReference_image.child("profile_photos").child(uri.getLastPathSegment()); 
     filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
      @Override 
      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
       Toast.makeText(UserProfileActivity.this,"Upload Complete",Toast.LENGTH_SHORT).show(); 
       progressDialog.dismiss(); 
      } 
     }); 
    } 
} 
+0

检查我答,问我,如果你有任何查询 –

+0

如果你的进度对话框不显示,它看起来像你的'onActivityResult'甚至没有被调用。你是否在调试器下运行代码来验证你是否进入了'onActivityResult'? –

+0

是的。我在获得意图后调用了错误的方法@FrankvanPuffelen –

回答

0

试试这个

public class MainActivity extends AppCompatActivity { 
Button chooseImg, uploadImg; 
ImageView imgView; 
int PICK_IMAGE_REQUEST = 111; 
Uri filePath; 
ProgressDialog pd; 

//creating reference to firebase storage 
FirebaseStorage storage = FirebaseStorage.getInstance(); 
StorageReference storageRef = storage.getReferenceFromUrl("gs://fir-example-c4312.appspot.com"); //change the url according to your firebase app 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    chooseImg = (Button)findViewById(R.id.chooseImg); 
    uploadImg = (Button)findViewById(R.id.uploadImg); 
    imgView = (ImageView)findViewById(R.id.imgView); 

    pd = new ProgressDialog(this); 
    pd.setMessage("Uploading...."); 


    chooseImg.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_PICK); 
      startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE_REQUEST); 
     } 
    }); 

    uploadImg.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(filePath != null) { 
       pd.show(); 

       StorageReference childRef = storageRef.child("image.jpg"); 

       //uploading the image 
       UploadTask uploadTask = childRef.putFile(filePath); 

       uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
        @Override 
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
         pd.dismiss(); 
         Toast.makeText(MainActivity.this, "Upload successful", Toast.LENGTH_SHORT).show(); 
        } 
       }).addOnFailureListener(new OnFailureListener() { 
        @Override 
        public void onFailure(@NonNull Exception e) { 
         pd.dismiss(); 
         Toast.makeText(MainActivity.this, "Upload Failed -> " + e, Toast.LENGTH_SHORT).show(); 
        } 
       }); 
      } 
      else { 
       Toast.makeText(MainActivity.this, "Select an image", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 
} 

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

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { 
     filePath = data.getData(); 

     try { 
      //getting image from gallery 
      Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath); 

      //Setting image to ImageView 
      imgView.setImageBitmap(bitmap); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

}

不要忘了火力控制台

更改规则默认情况下我们是不允许不经认证访问火力存储。使用任何网络浏览器转到Firebase控制台。打开您在上述步骤中使用的Firebase项目,然后转到“存储”,然后选择“规则”选项卡。现在将读取和写入规则更改为true,如下图所示。

enter image description here

+0

中查看,OP已在使用Firebase存储。 –

0

试试这个方法,它的工作对我来说

里面onActivityResult,

 StorageReference storageRef = storage.getReferenceFromUrl(---STORAGE_BUCKET---); 
        StorageReference imagePathReference = storageRef.child("image"); 
        final Uri uri = data.getData(); 
        Bitmap bmp = null; 
        try { 
         bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData()); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 

     // convert bitmap to byte array to save image in firebase storage 
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
        if (bmp != null) { 
         bmp.compress(Bitmap.CompressFormat.JPEG, 60, bos); 
        } 
        byte[] dataNew = bos.toByteArray(); 


    uploadTask = imagePathReference.putBytes(dataNew); 
        try { 
         uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
          @Override 
          public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
          // success 
          } 

         }); 
        }catch (Exception e){ 

        }