简体   繁体   中英

Uploading Multiple Files in Android using existing multipart post project

I am using a project which upload just one image or video file. http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/ downloaded here. I modified the MainActivity

File f = new File(Environment
                    .getExternalStorageDirectory(),
                    Config.IMAGE_DIRECTORY_NAME);

            File[] file = f.listFiles();
            String s= null;
            arrayFiles = new ArrayList<String>();
             if (file.length == 0)
             {
                 Toast.makeText(getApplicationContext(), "No Files Found", Toast.LENGTH_LONG).show();
                 finish();
             }

                else {
                    for (int i=0; i<file.length; i++) 
                    {
                        arrayFiles.add(file[i].getName());
                        s=arrayFiles.get(i);
                     Toast.makeText(getApplicationContext(),s , Toast.LENGTH_SHORT).show();
                }
                     Intent i= new Intent(MainActivity.this, UploadActivity.class);
                     i.putStringArrayListExtra("files", arrayFiles);
                     startActivity(i);      

This gets me all the files complete path. In uploadActvity

 for (int j = 0; j < files.size(); j++) {
                //filePath=Environment.getExternalStorageDirectory()+"/"+Config.IMAGE_DIRECTORY_NAME+"/"+files.get(j);
                filePath=files.get(j);
                Toast.makeText(getApplicationContext(), filePath, Toast.LENGTH_SHORT).show();
                new UploadFileToServer().execute();
                filePath="";
            }

I am using this code for uploading all the files by getting the arrayList from intent and one by one copying path to filePath. But i get EISDIR java io file not found exception although the toast shows correct path sent from main activity. plz help

Add file path to constructor of UploadFileToServer like UploadFileToServer(filePath)

UploadFileToServer moves your work to background Thread an when this thread would start your filePath would be empty or some other file path. So, bind the file path with your AsyncTask.

Mistake lies where filePath is nulled. As Async task runs in background, so equating filePath to null gives file not found exception. Some sort of delay must be provided after new UploadFileToServer().execute(); to let the file upload complete and then load next file. Works!!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM