firebase 存储 - 使用 android 相机上传照片,但在将照片提交到 firebase 存储时崩溃

Posted

技术标签:

【中文标题】firebase 存储 - 使用 android 相机上传照片,但在将照片提交到 firebase 存储时崩溃【英文标题】:firebase storage - using android camera to upload photo but crashes when submitting the photo to firebase storage 【发布时间】:2018-02-14 04:53:55 【问题描述】:
public class BidInPutActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback 

Button submit;
EditText title;
Spinner catg;
EditText dscrp;
EditText qnt;
RadioGroup rptrq_rdo;
RadioButton yes_rdo;
RadioButton no_rdo;
EditText exprdt;
ImageButton upldbtn;
ImageView prdctimg;
StorageReference filepath;
Uri uri;
String bidPhotoUrl;
private static final int CAMERA_REQUEST_CODE = 1;
private static  final int REQUEST_READ_PERMISSION = 2;

DatabaseReference databaseBid;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bid_in_put_activity);

    filepath = FirebaseStorage.getInstance().getReference();

    databaseBid = FirebaseDatabase.getInstance().getReference("Bids");
    submit = (Button)findViewById(R.id.submit);
    title = (EditText)findViewById(R.id.title);
    catg = (Spinner)findViewById(R.id.catg);
    dscrp= (EditText)findViewById(R.id.dscrp);
    qnt= (EditText)findViewById(R.id.qnt);
    rptrq_rdo= (RadioGroup)findViewById(R.id.rptrq_rdo);
    yes_rdo=(RadioButton) findViewById(R.id.yes_rdo);
    no_rdo=(RadioButton) findViewById(R.id.no_rdo);
    exprdt= (EditText)findViewById(R.id.exprdt);
    upldbtn=(ImageButton)findViewById(R.id.upldbtn);
    prdctimg=(ImageView)findViewById(R.id.prdctimg);

    submit.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            addBid();
        
    );
//accessing camera intent and upload to firebase process

    upldbtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
           Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
           startActivityForResult(intent,CAMERA_REQUEST_CODE);
    private void addBid()
    final String bidtitle=title.getText().toString().trim();
    final String bidcategory=catg.getSelectedItem().toString().trim();
    final String biddescription=dscrp.getText().toString().trim();
    final String bidquantity=qnt.getText().toString().trim();
    final String bidrepetitive_requests_yes=yes_rdo.getText().toString().trim();
    final String bidrepetitive_requests_no=yes_rdo.getText().toString().trim();
    final String bidexpiry_date=exprdt.getText().toString().trim();
    if((!TextUtils.isEmpty(bidtitle))&&(!TextUtils.isEmpty(bidcategory))
            &&(!TextUtils.isEmpty(biddescription)) &&(!TextUtils.isEmpty(bidquantity))
            &&(!TextUtils.isEmpty(bidrepetitive_requests_yes))||(!TextUtils.isEmpty(bidrepetitive_requests_no))
            &&(!TextUtils.isEmpty(bidexpiry_date))||(!TextUtils.isEmpty(bidPhotoUrl)))

   final StorageReference onlinestorafephotoref = filepath.child("Photo").child(bidtitle).child(uri.getLastPathSegment());

        onlinestorafephotoref.putFile(uri).addOnSuccessListener(new 
        OnSuccessListener<UploadTask.TaskSnapshot>() 

        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) 

               bidPhotoUrl = taskSnapshot.getDownloadUrl().toString();
               String id = databaseBid.push().getKey();
               classBidAttributes bid = new classBidAttributes(id,bidtitle,bidcategory,biddescription,bidquantity,
                        bidrepetitive_requests_no,bidrepetitive_requests_yes,bidexpiry_date,bidPhotoUrl);

                databaseBid.child(id).setValue(bid);
                Toast.makeText(BidInPutActivity.this, "Bid added", Toast.LENGTH_SHORT).show();
            
        ).addOnFailureListener(new OnFailureListener() 
            @Override
            public void onFailure(@NonNull Exception e) 

                Toast.makeText(BidInPutActivity.this, "Submit Failure ! Please Try Again", Toast.LENGTH_LONG).show();
                bidPhotoUrl = "";
                String id = databaseBid.push().getKey();
                classBidAttributes bid = new classBidAttributes(id,bidtitle,bidcategory,biddescription,bidquantity,
                        bidrepetitive_requests_no,bidrepetitive_requests_yes,bidexpiry_date,bidPhotoUrl);
                databaseBid.child(id).setValue(bid);
            
        ); finish();*/

        Toast.makeText(BidInPutActivity.this, "Bid added", Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(BidInPutActivity.this, "Fill in missing data", Toast.LENGTH_LONG).show();
    

    public void onActivityResult(int requestCode, int resultCode, Intent data)
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK && data != null)
        uri = data.getData();
        Glide.with(BidInPutActivity.this).load(uri).into(prdctimg);
    


PS:如果我删除了所有与照片上传相关的代码部分,代码可以正常工作,因为它也将文本输入上传到firebase realtime database,我确保我的manifest 包含相机权限和read_external_storage 权限,并且我已确保在 ma​​rshmallow 的情况下允许访问相机,所以希望有人能提供帮助,因为这让我发疯了,如果你需要任何东西,请告诉我附加信息。

    ///////// CrashLog//////////

09-06 10:36:12.293 3616-3616/com.mrmazen.cbid E/androidRuntime: FATAL EXCEPTION: main
                                                            Process: com.mrmazen.cbid, PID: 3616
                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference
                                                                at com.mrmazen.cbid.BidInPutActivity.addBid(BidInPutActivity.java:122)
                                                                at com.mrmazen.cbid.BidInPutActivity.access$000(BidInPutActivity.java:30)
                                                                at com.mrmazen.cbid.BidInPutActivity$1.onClick(BidInPutActivity.java:75)
                                                                at android.view.View.performClick(View.java:5637)
                                                                at android.view.View$PerformClick.run(View.java:22429)
                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                at android.os.Looper.loop(Looper.java:154)
                                                                at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

【问题讨论】:

请粘贴崩溃日志 我刚刚添加了崩溃日志 好的,我已经能够找出问题的根源,它的 uri“uri.getLastPathSegment()”,我已经做了一个条件,如果 uri 为 null 来显示祝酒,而不是继续该过程,它告诉我 uri 为空,所以现在应用程序没有崩溃,但 uri 不工作,我仍然无法弄清楚如何让它工作 它肯定与 onActivityResult 方法有关,因为 uri = data.getData();并没有真正调用任何数据。 【参考方案1】:

请注意,没有记录从ACTION_IMAGE_CAPTURE 返回Uri,相机应用不需要在您的ACTION_IMAGE_CAPTURE Intent 中返回 Uri.Supply EXTRA_OUTPUT,在这种情况下,您知道图像应该放在哪里存储起来。

final Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            Uri pictureUri=getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
            //pass your uri as an Extra here
            pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,pictureUri);
            startActivityForResult(pictureIntent, REQUEST_IMAGE_CAPTURE);

onActivityResult 中检查null。一旦你将Uri 提供给Glide

        //get path here
          picturePath= pictureUri.getPath();

【讨论】:

以上是关于firebase 存储 - 使用 android 相机上传照片,但在将照片提交到 firebase 存储时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

使用 Firebase 的 Android 聊天应用程序,但未在 firebase 中存储数据

如何在 Android 上的 Firebase 存储中设置持久性?

使用 kotlin for android app 将 pdf 文件上传到 firebase 存储

android-将视频压缩到firebase存储

Android中的Firebase数据描述排序

如何将 FireBase 云消息传递令牌存储在 Android 的 firebase 数据库中?