拍摄照片并将其上传到Firebase无效

时间:2017-03-16 08:42:14

标签: android firebase firebase-storage

我尝试拍照并将其发送到Firebase存储,同时将图片显示在同一活动的ImageView上。但是,在拍完照片后,没有任何反应。甚至没有崩溃。帮助

public class ServerImage extends AppCompatActivity {
private Button mUploadBtn;
private ImageView mImageView;
private static final int CAMERA_REQUEST_CODE=1;
private StorageReference mStorage;
private ProgressDialog mProgress;

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

        mStorage=FirebaseStorage.getInstance().getReference();
        mUploadBtn =(Button)findViewById(R.id.upload);
        mImageView=(ImageView)findViewById(R.id.imageViewUpload);
        mProgress=new ProgressDialog(this);


        mUploadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, CAMERA_REQUEST_CODE);

            }
        });
}

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

    if(requestCode==CAMERA_REQUEST_CODE&&requestCode==RESULT_OK){
        mProgress.setMessage("Uploading...");
        mProgress.show();
        Uri uri=data.getData();

        StorageReference filepath=mStorage.child("Photos").child(uri.getLastPathSegment());
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                mProgress.dismiss();

                Uri downloadUri=taskSnapshot.getDownloadUrl();
                Picasso.with(ServerImage.this).load(downloadUri).fit().centerCrop().into(mImageView);

                Toast.makeText(ServerImage.this, "Uploaded", Toast.LENGTH_LONG).show();
            }
        });
    }
}
}

2 个答案:

答案 0 :(得分:0)

尝试在此处存储图像

public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
                Bitmap photo = (Bitmap) data.getExtras().get("data");


                mImageView.setImageBitmap(photo);
            }
            else if (requestCode == PICK_IMAGE_REQUEST && 
           resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {

                Uri uri = data.getData();

                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
                    mImageView.setImageBitmap(bitmap);


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

答案 1 :(得分:0)

 public  void takePicture( ) {

    Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, CAMERA_REQUEST_CODE);
}

//handling the image chooser 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 && data != null ) {

       Bitmap bitmap = (Bitmap) data.getExtras().get("data");
    imageView.setImageBitmap(bitmap); }
 private void uploadFile() {

        //displaying a progress dialog while upload is going on
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Uploading");
        progressDialog.show();

        StorageReference storageRef= FirebaseStorage.getInstance().getReference();

        StorageReference mountainsRef = storageRef.child("images/pc.jpg");

        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();
                progressDialog.dismiss();
            }
        });

}