我正在开发一个应用程序,我将拍摄照片并将其发布到我的Firebase数据库以及一些说明。我写了一些代码,但是当我拍照并点击“确定”按钮时,应用程序崩溃了。
以下是我的代码:
package com.example.user.potholereportingapp;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Date;
import static android.app.Activity.RESULT_OK;
import static android.app.Activity.RESULT_OK;
import static android.app.Activity.RESULT_OK;
import static android.app.Activity.RESULT_OK;
import static android.app.Activity.RESULT_OK;
import static android.app.Activity.RESULT_OK;
public class PostActivity extends AppCompatActivity {
private ImageButton mSelectImage;
private EditText mPostTitle;
private EditText mPostDesc;
private Button mSubmitBtn;
private Uri photo=null;
private StorageReference mStorage;
private DatabaseReference mDatabase;
private ProgressDialog mProgress;
static final int CAM_REQUEST=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
mStorage= FirebaseStorage.getInstance().getReference();
mDatabase= FirebaseDatabase.getInstance().getReference().child("Blog");
mProgress=new ProgressDialog(this);
mSelectImage=(ImageButton) findViewById(R.id.imageSelect);
mPostTitle=(EditText)findViewById(R.id.titleField);
mPostDesc=(EditText)findViewById(R.id.descField);
mSubmitBtn=(Button)findViewById(R.id.submitBtn);
mSelectImage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, CAM_REQUEST);
}
}
});
mSubmitBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
startPosting();
}
});
}
private void startPosting() {
mProgress.setMessage("Posting Image...");
mProgress.show();
final String title_val = mPostTitle.getText().toString().trim();
final String desc_val = mPostDesc.getText().toString().trim();
if(!TextUtils.isEmpty(title_val)&&!TextUtils.isEmpty(desc_val) && photo != null){
mProgress.show();
StorageReference filepath = mStorage.child("Blog_Images").child(photo.getLastPathSegment());
filepath.putFile(photo).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl =taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
newPost.child("title").setValue(title_val);
newPost.child("desc").setValue(desc_val);
newPost.child("image").setValue(downloadUrl.toString());
mProgress.dismiss();
startActivity(new Intent(PostActivity.this,MainActivity.class));
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==CAM_REQUEST && resultCode==RESULT_OK) {
photo = data.getData();
mSelectImage.setImageURI(photo);
CropImage.activity(photo)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
mSelectImage.setImageURI(resultUri);
photo= resultUri;
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}
答案 0 :(得分:0)
我研究了我遇到的问题,然后我才知道如何做到这一点。 1.首先,我决定将我的照片保存在我的外部文件中。 2.其次,我将外部文件中的照片上传到firebase数据库。
以下是我的代码:
package com.example.user.potholereportingapp;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.File;
import java.util.Date;
public class PostActivity extends AppCompatActivity {
private ImageButton mSelectImage;
private EditText mPostTitle;
private EditText mPostDesc;
private Button mSubmitBtn;
private String file=null;
private StorageReference mStorage;
private DatabaseReference mDatabase;
private ProgressDialog mProgress;
static final int CAM_REQUEST=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
mStorage= FirebaseStorage.getInstance().getReference();
mDatabase= FirebaseDatabase.getInstance().getReference().child("Blog");
mProgress=new ProgressDialog(this);
mSelectImage=(ImageButton) findViewById(R.id.imageSelect);
mPostTitle=(EditText)findViewById(R.id.titleField);
mPostDesc=(EditText)findViewById(R.id.descField);
mSubmitBtn=(Button)findViewById(R.id.submitBtn);
mSelectImage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent camera_intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file=getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent,CAM_REQUEST);
}
});
mSubmitBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
startPosting();
}
});
}
private File getFile(){
File folder=new File("sdcard/camera_app");
if(!folder.exists()){
folder.mkdir();
}
File image_file=new File(folder,"cam_image.jpg");
return image_file;
}
private void startPosting() {
mProgress.setMessage("Posting Image...");
mProgress.show();
final String title_val = mPostTitle.getText().toString().trim();
final String desc_val = mPostDesc.getText().toString().trim();
if(!TextUtils.isEmpty(title_val)&&!TextUtils.isEmpty(desc_val) && file != null){
mProgress.show();
StorageReference filepath = FirebaseStorage.getInstance().getReference("images");
Uri file = Uri.fromFile(new File("sdcard/camera_app/"));
StorageReference riversRef = filepath.child("file"+new Date().getTime());
UploadTask uploadTask = riversRef.putFile(file);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Toast.makeText(getApplicationContext(),"Sending failed", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl =taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
newPost.child("title").setValue(title_val);
newPost.child("desc").setValue(desc_val);
newPost.child("image").setValue(downloadUrl.toString());
mProgress.dismiss();
Toast.makeText(PostActivity.this,"Uploading Finished...",Toast.LENGTH_LONG).show();
startActivity(new Intent(PostActivity.this,MainActivity.class));
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==CAM_REQUEST && resultCode==RESULT_OK) {
file="sdcard/camera_app/";
mSelectImage.setImageDrawable(Drawable.createFromPath(file));
}
}
}