尝试调用虚方法' java.lang.String android.net.Uri.getLastPathSegment()'在null对象引用上

时间:2016-10-14 18:19:24

标签: firebase android-image firebase-storage

我尝试从手机摄像头捕获后将图像上传到firebase存储,但是当我运行我的代码时,我收到了上述错误。我试图检查返回的URI是否为null,如此行代码所示

Toast.makeText(UploadImage.this, "YOUR URI IS NULL", Toast.LENGTH_LONG).show();

但是没有显示Toast消息,但是在toast之后的行中抛出了错误

filepath = storageReference.child("PhotoModel").child(uri.getLastPathSegment());

以下是我的整个代码,

public class UploadImage extends AppCompatActivity {

private Button mUploadImg;
private ImageView mImageView;
private StorageReference storageReference, filepath;
private ProgressDialog progressDialog;
private static final int CAMERA_REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload_image);
    storageReference = FirebaseStorage.getInstance().getReference();

    mUploadImg = (Button) findViewById(R.id.btnUploadImg);
    mImageView = (ImageView) findViewById(R.id.imgView);
    progressDialog = new ProgressDialog(this);
    mUploadImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

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

        }
    });

}

@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("Upload Image ...");
        progressDialog.show();

        Uri uri = data.getData();
        if (uri == null) {


            Toast.makeText(UploadImage.this, "YOUR URI IS NULL", Toast.LENGTH_LONG).show();
        }
        filepath = storageReference.child("PhotoModel").child(uri.getLastPathSegment());
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressDialog.dismiss();

                Toast.makeText(UploadImage.this, "Uploading ...", Toast.LENGTH_LONG).show();
            }
        });

    } else {
        Toast.makeText(UploadImage.this, "Upload Failed...Check your Internet Connection", Toast.LENGTH_LONG).show();
    }
  }
 }

有人帮我弄清问题的起点,因为我现在无法真正指责它......

1 个答案:

答案 0 :(得分:1)

有一种不同的方式来实现此功能。希望它可以帮到某人。

首先: onCreate 方法中。 假设您已经拥有 ImageButton (将调用相机)并上传 按钮 (将图片上传到firebase存储)在xml文件中定义,初始化 ImageButton

/** Button for adding and Uploading image*/
    mUploadImg = (ImageButton) findViewById(R.id.btnUploadImg);

第二: 图像按钮

设置onclick侦听器
        /** Capture Image save and pass it to an intent   */
    mUploadImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

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

第三: onCreate 方法之外,实施 startActivityForResult 我用过 位图 不是 Uri

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
        //set the progress dialog
        progressDialog.setMessage("Uploding image...");
        progressDialog.show();

        //get the camera image
        Bundle extras = data.getExtras();
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] databaos = baos.toByteArray();

        //set the image into imageview
        mImageView.setImageBitmap(bitmap);
      //String img = "fire"

        //Firebase storage folder where you want to put the images
        StorageReference storageRef = FirebaseStorage.getInstance().getReference();

        //name of the image file (add time to have different files to avoid rewrite on the same file)
        StorageReference imagesRef = storageRef.child(productname + new Date().getTime());
        //send this name to database
        //upload image
        UploadTask uploadTask = imagesRef.putBytes(databaos);
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(UploadImage.this, "Sending failed", Toast.LENGTH_SHORT).show();
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                productDownloadUrl = taskSnapshot.getDownloadUrl().toString();
                 progressDialog.dismiss();
                }
        } );}
}

如果这对你有所帮助,请记得承认我的回答。