0

I'am trying to upload image to firebase storage but in vain .

When I add method of on upload success or any another method that app crash but without them the app upload the image without problems.

I'am sure that firebase is true.

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;
}}



}

//this is my class path

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

//this is my firebase storage

compile 'com.google.firebase:firebase-storage:11.2.0'
user11019720
  • 43
  • 1
  • 4
  • 1
    Could you post a stacktrace or a logcat of the error ? – Arthur Attout Apr 02 '19 at 11:43
  • My logcat didn't show any thing I use aide on my phone and my ram is low so aide didn't show any thing in logcat and I didn't know where is the problem – user11019720 Apr 02 '19 at 11:58
  • Without any stacktrace, or any form of error report, that will be hard to debug ... Could you plug in your device via USB to your computer, and check the logcat from there ? – Arthur Attout Apr 02 '19 at 12:08
  • I will try to show the logcat – user11019720 Apr 02 '19 at 12:35
  • This won't work anymore: `taskSnapshot.getDownloadUrl().toString()`. The `getDownloadUrl()` method no longer returns the actual URL, but now returns a task that resolves to the URL when it completes. For the up to date code, see the [documentation](https://firebase.google.com/docs/storage/android/download-files#download_data_via_url) or this answer: https://stackoverflow.com/questions/51056397/how-to-use-getdownloadurl-in-recent-versions/51064689#51064689 – Frank van Puffelen Apr 02 '19 at 13:26

0 Answers0