2017-09-05 89 views
0
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許可,而且我確信,訪問相機允許在棉花糖的情況下,所以希望有人幫好,因爲這是推動我堅果,告訴我你是否需要任何額外的信息。火力存儲 - 提交照片,以火力存儲在使用Android攝像頭上傳的照片,但崩潰

///////// 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) 
+0

,請複製粘貼崩潰日誌 – Godather

+0

我剛加入的系統崩潰日誌 – user3207637

+0

好吧,我已經能夠找出其中是問題的根源,它的uri「uri.getLastPathSegment()」,我已經提出了一個條件,如果uri爲空來顯示敬酒而不是繼續進程並且它告訴我uri爲空,所以現在應用程序不會崩潰,但uri不工作,我仍然是你nable來弄清楚如何使它工作,雖然 – user3207637

回答

0

請注意得到一個Uri回從ACTION_IMAGE_CAPTURE未記錄,和攝像頭應用程序並不需要在你的ACTION_IMAGE_CAPTURE意圖返回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();