How to store downloaded images in to android SD card - android

Hi I am developing app which downloads the images from the web site
and then i am displaying them as slide show. Now I want save the
downloaded images into my SD card please help me.
My current attempt is:
File imageFileFolder = new File(Environment
.getExternalStorageDirectory(), "test");
imageFileFolder.mkdir();
File imageFileName = new File(imageFileFolder, date
+ pBean.getAuthorName());
InputStream fis = pBean.getInputStream();
byte[] data = new byte[fis.available()];
fis.read(data);
FileOutputStream fos = new FileOutputStream(imageFileName);
fos.write(data);
fos.close();
fis.close();

There are lot of examples available for this
Check this post
Lazy load of images in ListView
and link
http://open-pim.com/tmp/LazyList.zip

In your AndroidManifest.xml you'll need to add the permission to write
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Then just get get the path to your filename and make sure your directories exist before trying to write the file to the sdcard.
String imageFileName = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/somedirectory/imagename.jpg"

Related

Can't see the bitmap I saved to new directory

I was looking for a way to save a bitmap to a folder (with my app's name), in such a way the default gallery will recognize it.
So I managed to save the image, but I can't see it either from the gallery or my PC (using explorer)
this is my code:
// Save bitmap to internal memory
private void savePhoto(Bitmap bmp){
String appName = "myApp";
String file_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + appName;
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
// Image file
File file = new File(dir, "IMG" + "_" + System.currentTimeMillis() + ".jpg");
FileOutputStream out = null;
try
{
out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
out = null;
} catch (Exception e)
{
e.printStackTrace();
}
}
I can see it using some File Manager installed on my galaxy, but it doesn't help a lot.
I know I should inform the gallery using a media scanner (which is another bridge I need to cross) but may someone help me understand way I can't even find the file..should I change its visibility somehow?
and one more thing: I read at another question regarding the issue, that I should add metadata to the image/folder so that the gallery would show it. Is it necessary?
Many thanks!!!
Apparently it did have something to do with the media scanner, I added this line
MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null, null);
and now it works.
from the documentation:
MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider.
try to replace this getExternalStoragePublicDirectory(....)
with this one getExternalStorageDirectory();

Path of PDF Viewer Library

I am using PDF Viewer Library to view PDF inside an Android app. I've browsed through a lot of tutorials, one of which is Dipak's tutorial. I want to access a PDF file stored in my assets folder instead of accessing the external storage. My problem is, I can't get the "path" right. It always return file not found.
I've tried the following, still yields the same result:
this.getAssets()
file:///android_assets/file_name.pdf
file:///android_asset/file_name.pdf
You can't get path from asset, have to right in sd or internal memory to get the path.
For SD Card
First Take Permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
then write it in card
File f = new File(Environment.getExternalStorageDirectory() + "file.pdf");
if (!f.exists()) try {
InputStream is = getAssets().open("file.pdf");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (Exception e) { throw new RuntimeException(e); }
Staring path = f.getPath();

Android - downloading file, java.io.FileNotFoundException

I may be way off here, but I am trying to download a file and store it in the downloads folder on my phone. I am getting a "java.io.FileNotFoundException" error, because the file doesn't exist, because I'm trying to download it...what am I doing wrong?
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File dir = new File(PATH);
dir.mkdirs();
File outputFile = new File(dir, "downloadFile");
FileOutputStream fos = new FileOutputStream(outputFile);
This fails, with the following:
java.io.FileNotFoundException: /mnt/sdcard/download/downloadFile (Permission denied)
I am using the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions....
Please try following updated code,
String PATH = Environment.getExternalStorageDirectory() + "/download";
File dir = new File(PATH);
dir.mkdirs();
File outputFile = new File(dir, "downloadFile");
if ( !outputFile.exists() )
{
outputFile.create();
}
FileOutputStream fos = new FileOutputStream(outputFile);
There is a "/" after the download. This way Android is thinking that you are creating Recursive Directory. While using mkdirs(), you can not create recursive directories.
You can also check my answer for same in Java ME here.
Add Permission to write external memory in your Manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Here you want to make directoy?
it there is already folder of download than use this code directly.
// create a File object for the parent directory
File Directory = new File("/sdcard/download/");
Directory.mkdirs();
// create a File object for the output file
File outputFile = new File(Directory, downloadFile);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
It might be wise to use Environment.getExternalStorageDirectory() for getting the "SD Card" directory as this might change

Convert View to bitmap FileNotFoundException problem

I'm trying to convert my LinearLayout to a bitmap so that I can save the current layout content as an image in SD card. First, I create bitmap and canvas and attach the layout to the canvas. Followed steps from http://www.brighthub.com/mobile/google-android/articles/30676.aspx#comments.
//code to add child view into layout before creating bitmap
screenBitmap = Bitmap.createBitmap(200,200,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(screenBitmap);
layout.draw(canvas);
When I press the save button, it should save the current layout as an image to SD card. Here are my steps:
FileOutputStream outStream = null;
File file = new File("/sdcard/Health Management System/");
file.mkdirs();
File outputFile = new File(file, fileName);
outStream = new FileOutputStream(outputFile);
BufferedOutputStream bos = new BufferedOutputStream(outStream);
bos.flush();
bos.close();
screenBitmap.compress(Bitmap.CompressFormat.PNG, 100,bos);
It can create folder in SD card but no file created under this folder. It always gives me FileNotFoundException. I'm not sure it is the file creating problem or the screenBitmap problem. Can anyone give me some clue? Thanks!
Have you enabled the right permissions in the Android Manifest? i.e. android.permission.WRITE_EXTERNAL_STORAGE.
I was getting the same FileNotFoundException when trying to save to SD before adding the permission.
After
File outputfile = new File(file, filename);
Insert this:
outputfile.createNewFile();

Android: how to delete internal image file

What i want to do: delete an image file from the private internal storage in my app. I save images in internal storage so they are deleted on app uninstall.
I have successfully created and saved:
String imageName = System.currentTimeMillis() + ".jpeg";
FileOutputStream fos = openFileOutput(imageName, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 35, fos);
an image that i receive through
bitmap = BitmapFactory.decodeStream(inputStream);
I am able to retrieve the image later for display:
FileInputStream fis = openFileInput(imageName);
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
DataOutputStream outWriter = new DataOutputStream(bufStream);
int ch;
while((ch = fis.read()) != -1)
outWriter.write(ch);
outWriter.close();
byte[] data = bufStream.toByteArray();
bufStream.close();
fis.close();
imageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
I now want to delete this file permanently. I have tried creating a new file and deleting it, but the file is not found:
File file = new File(imageName);
file.delete();
I have read on the android developer website that i must open private internal files using the openFileInput(...) method which returns an InputStream allowing me to read the contents, which i don't really care about - i just want to delete it.
can anyone point me in the right direction for deleting a file which is stored in internal storage?
Erg, I found the answer myself. Simple answer too :(
All you have to do is call the deleteFile(imageName) method.
if(activity.deleteFile(imageName))
Log.i(TAG, "Image deleted.");
Done!

Categories

Resources