上传图片时Firebase存储崩溃

时间:2019-04-02 11:39:38

标签: java firebase firebase-storage

我正在尝试将图像上传到Firebase存储,但徒劳无功。

当我添加成功上传方法或任何其他方法导致应用程序崩溃但没有它们时,应用程序上传图像就没有问题。

我确定firebase是真实的。

public class add extends Activity
{
    private Button button1;
    private ImageView im;
    private EditText edit1,edit2;

    private String im_link,im_name;
    public final int REQ_CD_PICK = 101;
    private Intent pick = new Intent(Intent.ACTION_GET_CONTENT);
    private int k;

    private HashMap<String, Object> map = new HashMap<>();
    private DatabaseReference post;
    private StorageReference up = FirebaseStorage.getInstance().getReference("up");

    private ProgressBar prog;
    private Calendar date = Calendar.getInstance();

       @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user);
        FirebaseApp.initializeApp(this);
        edit1=findViewById(R.id.edit1);
        edit2=findViewById(R.id.edit2);
        button1=findViewById(R.id.btn);
        pick.setType("image/*");
        pick.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        im=findViewById(R.id.userImageView);

            post=FirebaseDatabase.getInstance().getReference().child("post");

        im.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                startActivityForResult(pick, REQ_CD_PICK);
            }
        });

            button1.setOnClickListener(new View.OnClickListener(){
                    public void onClick(View view){
                        if(edit1.getText().length()<15){
                            edit1.setError("ادخل العنوان");
                        }
                        else if(edit2.getText().length()<50){
                            edit2.setError("أدخل نص المقال");
                        }else{
                            if(k>0){
                            edit1.setVisibility(View.GONE);
                            edit2.setVisibility(View.GONE);
                            im.setVisibility(View.GONE);
                            button1.setVisibility(View.GONE);
                            up.child(im_name).putFile(Uri.fromFile(new File(im_link)))




                                .addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception exception) {
                                            // Handle unsuccessful uploads
                                        }
                                    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                        @Override
                                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                            final String _downloadUrl = taskSnapshot.getDownloadUrl().toString();
                                            map = new HashMap<>();
                                            map.put("link",_downloadUrl);
                                            map.put("title",edit1.getText());
                                            map.put("text",edit2.getText());
                                            map.put("date", new SimpleDateFormat("dd/MM/yyy").format(date.getTime()));
                                            post.updateChildren(map);

                                            Toast.makeText(add.this,"تم النشر",Toast.LENGTH_LONG)
                                                .show();
                                            map.clear();
                                        }
                                    });

                    }}}});
}

    @Override
    protected void onActivityResult(int _requestCode, int _resultCode, Intent _data) {
        super.onActivityResult(_requestCode, _resultCode, _data);

        switch (_requestCode) {
            case REQ_CD_PICK:
                if (_resultCode == Activity.RESULT_OK) {
                    ArrayList<String> _filePath = new ArrayList<>();
                    if (_data != null) {
                        if (_data.getClipData() != null) {
                            for (int _index = 0; _index < _data.getClipData().getItemCount(); _index++) {
                                ClipData.Item _item = _data.getClipData().getItemAt(_index);
                                _filePath.add(FileUtil.convertUriToFilePath(getApplicationContext(), _item.getUri()));
                            }
                        }
                        else {
                            _filePath.add(FileUtil.convertUriToFilePath(getApplicationContext(), _data.getData()));
                        }
                    }
                    k = 1;
                    im.setImageBitmap(FileUtil.decodeSampleBitmapFromPath(_filePath.get((int)(0)), 1024, 1024));
                    im_link = _filePath.get((int)(0));
                    im_name = Uri.parse(_filePath.get((int)(0))).getLastPathSegment();

                    }
                else {

                }
                break;
                default:
                break;
}}



}

//这是我的课程路径

classpath'com.google.gms:google-services:3.0.0'

//这是我的Firebase存储

compile 'com.google.firebase:firebase-storage:11.2.0'

0 个答案:

没有答案