简体   繁体   中英

Getting an invalid argument error while trying to download file from a server in my app

I am trying to get a file from the server and store it in the sd card in my app. I am getting an invalid argument error.

Please see the attached code and logcat below. Kindly help

Also, is there any better way to do this. I frequently get socket timeout exception as well

File file = new File(getExternalFilesDir(null),values);

                URL url = new URL("http://abcd.com/edmuploads/"+values);
                URLConnection urlConnection = url.openConnection();
                urlConnection.setReadTimeout(7000);
                urlConnection.setConnectTimeout(7000);

                InputStream inputStream = urlConnection.getInputStream();
                BufferedInputStream bufferinstream = new BufferedInputStream(inputStream);

                ByteArrayBuffer baf = new ByteArrayBuffer(5000);
                int current = 0;
                while((current = bufferinstream.read()) != -1){
                    baf.append((byte) current);
                }

//This is where I am getting an invalid  argument error. The line number 560 as mentioned in the logs below
                FileOutputStream fos = new FileOutputStream(file, false); 

                    fos.write(baf.toByteArray());
                    fos.flush();
                    fos.close();

Log cat

08-06 10:15:37.089: W/System.err(11448): java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.veleztechnologies.eldicciomalopremium/files/1375701341.3gp
08-06 10:15:37.089: W/System.err(11448):  (Invalid argument)
08-06 10:15:37.099: W/System.err(11448):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
08-06 10:15:37.099: W/System.err(11448):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
08-06 10:15:37.099: W/System.err(11448):    at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
08-06 10:15:37.099: W/System.err(11448):    at com.veleztechnologies.eldicciomalopremium.WordListActivity.getFileList(WordListActivity.java:560)

It seems you can't write to files that don't exist in this case. Instead, use:

if(!file.exists()) {
    file.createNewFile();
}

EDIT: as for better methods for downloading files, this is what I use:

URL website = new URL("http://url");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

For the FileOutputStream, you might need to use an absolute path.

将此权限添加到您的清单文件中

在清单文件中添加WRITE_EXTERNAL_STORAGE PERMISSION

I finally found a solution to this. Was a problem with special character in the filename. There was a newline character at the end of the filename, which was appended by mistake, when the file was created in the first place. Removing that fixed this.Thanks everyone.

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