Skip to content

Easily upload files in the background with Event Listener

License

Apache-2.0, Apache-2.0 licenses found

Licenses found

Apache-2.0
LICENCE
Apache-2.0
LICENSE
Notifications You must be signed in to change notification settings

seifhjh/AndroidUploadManager

N|Solid

Android Upload Manager

ScreenShot

N|Solid

Download

Step 1. Add the JitPack repository to your build file
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.seifhjh:AndroidUploadManager:1.2.0'
}

How to use

Client (Android)

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        UploadManager.urlServer="http://host/uploadFile";
    }

One file

 UploadManager.uploadFileFromFile(getApplicationContext(), file, new UploadManager.OnProgressListener() {
            @Override
            public void onProgress(long percent) {
                Log.e(TAG,"onProgress : "+percent);
            }

            @Override
            public void onComplite(String response) {
                Log.e(TAG,"onComplite : "+response);
            }

            @Override
            public void onFailed(Throwable error) {
                Log.e(TAG,"onFailed : "+error.getMessage());
            }
        });

Multi files

  List<File> listFiles=new ArrayList<>();
        listFiles.add(file);
        listFiles.add(file);
        UploadManager.uploadFileFromFile(getApplicationContext(), listFiles, new UploadManager.OnProgressMultiListener() {
            @Override
            public void onProgress(long percent) {
                Log.e(TAG,"onProgress : "+percent);
            }

            @Override
            public void onComplite(List<String> listResponse) {
                Log.e(TAG,"onComplite : "+listResponse);
            }

            @Override
            public void onFailed(Throwable error) {
                Log.e(TAG,"onFailed : "+error.getMessage());
            }
        });

Server

<?php
include "conn.php";
try{
    $name = time();
    $path = $_FILES['file']['name'];
    
    $ext = pathinfo($path, PATHINFO_EXTENSION);
    $stored_name ="file-$name.$ext";
    if (!file_exists('path/file')) {
        mkdir('path/file/', 0777, true);
        }
        
    if(move_uploaded_file($_FILES['file']['tmp_name'],"path/file/$stored_name"))
    {
        mime_content_type('path/file/'.$stored_name);

        $result=array(
            'master'=>array(
            'etat' => "succes",
            'url'=> "$host/path/file/$stored_name"
            ));
    }else{
        $result=array(
            'master'=>array(
            'etat' => "failure",          
            'url'=> "$host/path/file/profil-default.jpg"
            ));
    }

    echo json_encode($result);

}catch(Exception $e){
  
}		

?>

Output

{
"master":
{"etat":"succes","url":"http:\/\/host\/path\/file\/file-1563922243.mp4"}
}

Full Example

MainActivity.java

Developer

Seif Hadjhassen - Github
Seif Hadjhassen - Linked In
Seif Hadjhassen - Facebook
Seif Hadjhassen - Twitter
Seif Hadjhassen - Dribbble
Seif Hadjhassen - Pinterest

License

  Copyright 2019 Seif Hadjhassen

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.