I am trying to save a stream to a subfolder of storage/emulated/0, but am getting the error fail readDirectory() errno=20
I am using the below code :
BufferedInputStream bis = new BufferedInputStream(instream, buffersize);
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(fileName + ".download"),
buffersize);
int len;
int downloadedlen = 0;
byte[] buff = new byte[buffersize];
String firstfewchars = null;
while ((len = bis.read(buff)) > 0) {
Log.i(TAG, "Writing Data from Stream Line 814");
out.write(buff, 0, len);
}
Can anyone suggest what is wrong ?
Thanks.
Are you sure you have permissions, and folder is public?
For testing, You can try get directory in this way:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
Also, don't forget permissions in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Related
I'm trying to convert an existing CSV file to a gzip file.
I verified the CSV looks good. Once I run this code, I get a "failed to expand" error and tried an online decompression tool that also failed, so it seems the output zip is corrupt.
public void compressGzip(String input, String dest) throws IOException {
Path pathSource = Paths.get(input);
Path destSource = Paths.get(dest);
try (GZIPOutputStream gos = new GZIPOutputStream(
new FileOutputStream(destSource.toFile()));
FileInputStream fis = new FileInputStream(pathSource.toFile())) {
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
gos.write(buffer, 0, len);
}
}
}
Anything I could be missing here?
I have developed a method to zip a file that will take file path and filename as a parameter and will zip a file as shown below could you please advise how can I modify this method to be more efficient and more fast as I am a big fan of optimization..
public File generateZipForAFile(String folderPath, String reportFileName)
throws FileNotFoundException, IOException {
File inputFile = new File(folderPath + reportFileName);
FileInputStream in = new FileInputStream(inputFile);
File outputZipFile = new File(folderPath, reportFileName + ".zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputZipFile));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(reportFileName ));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
out.close();
in.close();
return outputZipFile;
}
There is not much you can do in your code. Furthermore, most of the time is spent while actually doing the zip.
So even if you reduce the time spent in your part to 0 the overall gain will be very small.
In my application, my requirement is need to install .APK file from assets folder, so that I am trying to copy the apk file from assets folder to sdcard, I get File Not Found Exception.
these the following code:
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath();
String file_name = "ImageDownloading.apk";
AssetManager assetManager = getAssets();
try{
InputStream input = new BufferedInputStream(assetManager.open(file_name));
File path = new File(file_path);
if(!path.exists()){
path.mkdirs()
}
File file = new File(path,file_name);
OutputStream output = new FileOutputStream(file); // Here i get File Not Found Exception error.
byte data[] = new byte[1024];
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
catch(FileNotFoundException e){
Toast.makeText(MainActivity.this,"File not found exception " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
I have spent a lot of time but i did not find out the solution.
Do you have this permission set in your manifest file?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I want to download the image from reome URL and I get SSL error
String imgURL="http://whootin.s3.amazonaws.com/uploads/upload/0/0/23/82/Note_03_26_2013_01_10_55_68.jpg?AWSAccessKeyId=AKIAJF5QHW2P5ZLAGVDQ&Signature=Za4yG0YKS4%2FgoxSidFsZaAA8vWQ%3D&Expires=1364888750";
final ImageView ivCurrent;
ivCurrent = (ImageView)findViewById(R.id.imageView1);
// calling DownloadAndReadImage class to load and save image in sd card
DownloadAndReadImage dImage= new DownloadAndReadImage(imgURL,1);
ivCurrent.setImageBitmap(dImage.getBitmapImage());
The error:
javax.net.ssl.SSLException: Read error: ssl=0x19a4a0: I/O error during system call, Connection reset by peer
Your question make no sense because we know nothing about DownloadAndReadImage class, By the way I think you need to add these two permissions in your manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
P.S If you are looking for a great ImageLoder library, I suggest you Android Universal Image Loader:
https://github.com/nostra13/Android-Universal-Image-Loader
In my project I download and store images in SD card using InputStreams in the following way:
URL url = new URL(imageUrl);
InputStream input = url.openStream();
try {
// The sdcard directory e.g. '/sdcard' can be used directly, or
// more safely abstracted with getExternalStorageDirectory()
String storagePath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
int barIndex = imageUrl.indexOf("/");
String path = imageUrl.substring(barIndex + 1) + ".jpg";
String sdcardPath = storagePath + "/myapp/";
File sdcardPathDir = new File(sdcardPath);
sdcardPathDir.mkdirs();
OutputStream output = new FileOutputStream(sdcardPath + imagemId + ".jpg");
try {
byte[] buffer = new byte[4 * 1024];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
As #NullPointer pointed out, don't forget to check the manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You are Connect to an HTTPS/HTTP URL via and the SSL certificate provided by the site is not trusted by the devise you are running the code on.
setting up trust in the Apache HTTP Client.
I need to convert this File object to byte array:
File directory=new File(Environment.getExternalStorageDirectory() + "");
(I need only the names of folders and files on SDcard.)
I have already tried this:
byte[] send=null;
FileInputStream fis;
try {
fis = new FileInputStream(directory);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int readBytes = 0;
while(readBytes != -1)
{
readBytes = fis.read(buffer);
if(readBytes > 0)
{
bos.write(buffer, 0, readBytes);
}
else
break;
}
byte[] fileData = bos.toByteArray();
send=fileData;
But it returns this error:
java.io.FileNotFoundException: /mnt/sdcard (Is a directory)
You're trying to load the directory as if it were a file. It's not. What would you expect the contents of the byte array to be?
If you want to find the list of files in a directory, use File.listFiles().