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权限,我确保在 marshmallow 的情况下允许访问相机,所以希望有人能提供帮助,因为这让我疯了,告诉我如果你需要任何其他信息。
///////// 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 :(得分:0)
请注意,Uri
从[{1}}返回ACTION_IMAGE_CAPTURE
没有记录,相机应用也无需在EXTRA_OUTPUT
意图中返回Uri.Supply ACTION_IMAGE_CAPTURE
在哪种情况下,您知道应该存储图像的位置。
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();