Android getting FileNotFound Exception, even if file exists - android

As the title suggests I am getting "FileNotFoundException" even if image exists in SDCard, and even with all my efforts I am not able to find any reason for it. I have the permission in manifest for writing on external storage. Also this occurs for only certain images, so it's quite complicating the issue.
The path of the image is: /mnt/sdcard/projFolder/1090901/-23686809809
Following is the code to download image :
URL Url = new URL(url);
URLConnection urlConn = Url.openConnection();
if(!enoughSpaceLeft(urlConn.getContentLength())){
onError(NoSpaceError);
break;
}
InputStream is = Url.openStream();
OutputStream os = new FileOutputStream(file);
byte[] b = new byte[1024];
int length;
while ((length = is.read(b)) != -1)
os.write(b, 0, length);
os.close();
is.close();
I am attaching the image that is creating problem , it's actually a QR code

You should have write-to-SD card permissions.

One possible reason is that it won't be possible to read from the SD card if the phone is connected to a computer and mass storage mode is on.

Related

Android I saved a photo in removable storage using SAF, but Gallery don't recognize it and I cannot open it

I finally figure out how to save a photo in an arbitrary location in removable storage in Android by using a storage access framework.
However, I encounter another problem.
I successfully saved a photo in a path like /storage/6265-6530/DCIM/Camera but I cannot see the photo in Gallery. Moreover, I try to open the photo through the file browser but I cannot open it.
I connect the phone to the PC and I can open the photo I saved in PC.
So how to fix it?
below is my code:
DocumentFile pickedDir = DocumentFile.fromTreeUri(reactContext, mUri);
DocumentFile file = pickedDir.createFile("image", "myPhoto.jpg");
InputStream in = new FileInputStream(path);
OutputStream out = getContentResolver().openOutputStream(file.getUri());
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) { //read is successful
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,file.getUri()));
I have called sendBroadcast to scan the file but still, the gallery didn't show my photo.

Store remote PDF to Android Internal Storage

In my app I want to store a remote pdf to internal storage, so that no other application will have access to it. Can anyone please help.
Thanks in advance.
My code to store pdf file is :
File newdir = new File(getFilesDir().getAbsolutePath(),"/n.pdf");
newdir.mkdirs();
try {
FileOutputStream fos = new FileOutputStream(newdir);
URL url = new URL("my_pdf_path");
urlConnection = url.openConnection();
urlConnection.connect();
InputStream input = url.openStream();
byte[] buffer = new byte[1024];
int read;
while ((read = input.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
input.close();
} catch (Exception e) {
}
And when I try to access the above path, it says : "Error opening file. It does not exist or cannot be read".
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them. See http://developer.android.com/guide/topics/data/data-storage.html#filesInternal for full details. Read the answer to this question for a java example of how to download the file from the web. Android - downloading image from web, saving to internal memory in location private to app, displaying for list item

downloading terminates when App goes to background

I am working on an App, that uses phonegap.
When the App is started for the first time, I am downloading the data from the server.
That downloading process is being executed in AsyncTask, and in onPostExecute() of that, I am loading the url.
The problem being arose is that, when my download process is getting executed, and the App somehow goes to the background, the downloading gets terminated, unfortunately, which is absolutely not required.
To overcome this trouble; I guess, using service is one of the good options. But, in that case, another question arises, that is, how can I possibly, load the url inside service.
Otherwise, what else can be done?
Please, suggest me the possible ways to get rid off this trouble, I am currently facing.
Use the similar method for downloading the data. This may be helpful.
public void downloadUsingGET(String apkurl, String fileName) {
try {
String PATH = Environment.getExternalStorageDirectory()+"/Android/data/"+getPackageName()+"/files/";
URL url = new URL(apkurl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.connect();
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
} catch (IOException e) {
}
}
If you use a service, you can pass strings trough an intent(the intent you started the service).

Android Download Zip From Api And Store in SD CARD

I am working on an API which returns me a zip file containing multiple XML files, which i have to parse individually after extracting the zip file.
Here is the link for that(will download the zip-file) :
http://clinicaltrials.gov/ct2/results?term=&recr=&rslt=&type=&cond=&intr=&outc=&lead=&spons=&id=&state1=&cntry1=&state2=&cntry2=&state3=&cntry3=&locn=&gndr=Female&age=0&rcv_s=&rcv_e=&lup_s=&lup_e=studyxml=true
Here is my current code to save the zip-file in sdcard:
File root = Environment.getExternalStorageDirectory();
String url= "http://clinicaltrials.gov/ct2/results?term=&recr=&rslt=&type=&cond=&intr=&outc=&lead=&spons=&id=&state1=&cntry1=&state2=&cntry2=&state3=&cntry3=&locn=&gndr=Female&age=0&rcv_s=&rcv_e=&lup_s=&lup_e=xml=true";
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoInput(true);
conn.setConnectTimeout(10000); // timeout 10 secs
conn.connect();
InputStream input = conn.getInputStream();
FileOutputStream fOut = new FileOutputStream(new File(root, "new.zip"));
int byteCount = 0;
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = input.read(buffer)) != -1) {
fOut.write(buffer, 0, bytesRead);
byteCount += bytesRead;
}
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
Problem :
New.zip File is getting created in sdcard but it seems nothing is downloading also the file size is 0kb.
Is my code proper or I have to use something else to handel zipfiles.
Edit Solved :
I am extremely sorry the api link is invalid ... it should be
http://clinicaltrials.gov/ct2/results?term=&recr=&rslt=&type=&cond=&intr=&outc=&lead=&spons=&id=&state1=&cntry1=&state2=&cntry2=&state3=&cntry3=&locn=&gndr=Female&age=0&rcv_s=&rcv_e=&lup_s=&lup_e=&studyxml=true
& is required before studtxml..
Thnx every 1 for quick response ..
There is something wrong either in your .zip file URL or in .zip file size(0 byte size) because if we download this .zip file (From URL given by you) from web browser then also its downloaded with 0 byte size.
Downloaded .zip file URL.
Your Url in the code String url=... is not giving me a zip file.
http://clinicaltrials.gov/ct2/results?term=&recr=&rslt=&type=&cond=&intr=&outc=&lead=&spons=&id=&state1=&cntry1=&state2=&cntry2=&state3=&cntry3=&locn=&gndr=Female&age=0&rcv_s=&rcv_e=&lup_s=&lup_e=xml=true
The link you provided is different
http://clinicaltrials.gov/ct2/results?term=&recr=&rslt=&type=&cond=&intr=&outc=&lead=&spons=&id=&state1=&cntry1=&state2=&cntry2=&state3=&cntry3=&locn=&gndr=Female&age=0&rcv_s=&rcv_e=&lup_s=&lup_e=studyxml=true
Looks like there's an error: lup_e=xml=true should be lup_e=studyxml=true

How to upload an sqlite file in an Android application from Linux home server

I am planning on writing a simple Android application that is like a small directory of professors. It will have their name, email, phone and a picture of them. I need to manually send an sqlite file from the server to the phone. I have been trying to research how to do this, but it looks like there are so many ways! I'm hoping someone can point me in the best direction!
The easiest way I can think of, is to open an URLConnection to your server, read the response and save it to your app's database directory (or SD card).
For example:
URL url = new URL("http://example.com/file.sqlite");
URLConnection conn = url.openConnection();
BufferedInputStream bin = new BufferedInputStream(conn.getInputStream());
FileOutputStream fos =
new FileOutputStream("/data/data/[your.pkg.name]/databases/file.sqlite");
byte[] buffer = new byte[1024];
int read = 0;
do {
read = bin.read(buffer, 0, buffer.length);
if (read > 0)
fos.write(buffer, 0, read);
} while (read >= 0);
After that you can open your database with a subclass of SQLiteOpenHelper or directly by calling SQLiteDatabase.openDatabase(..):
e.g.:
SQLiteDatabase.openDatabase("/data/data/[your.pkg.name]/databases/file.sqlite",
null, SQLiteDatabase.OPEN_READWRITE);

Categories

Resources