Convert View to bitmap FileNotFoundException problem - android

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();

Related

how can we store a bitmap image onto the mobile memory?

my bitmap image in the bitmap variable whose name is imageFile how we further give name this bitmap image and store it in my mobile memory
in this picture a function show which receive image file and convert it into bitmap
1- you have to find the path to where you wanna save your image.
2- open an OutputStream object and give it the file path.
3- save your bitmap to the output stream.
4- flush.
5- close.
// Get environment dir
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "MyImage"+".jpg"); // the File to save to
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), optons); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream

Save an image and get the saved location of image on android emulator

Actually I have retrieved image saved on facebook but i am not getting how to save image to my emulator and get the path location of saved image as i have to save the path in sqlite.
In most of the answer in stackoverflow,it is only describing about saving the image but not abut retrieving path
imageURL = "http://graph.facebook.com/"+id+"/picture?type=small";
bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, fileName+".jpg");
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
And i have even updated my manifest file
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
your file object already hold that information.
Get it this way:
file.getAbsolutePath()
Use this snippet hope this will solve your issue
MediaStore.Images.Media.insertImage(getContentResolver(), bitmapimage,
barcodeNumber + ".jpg Card Image", barcodeNumber
+ ".jpg Card Image");

Write media file to internal/external storage

I am currently trying to figure out a way to write a media file to internal/external storage (primary storage). The file to be saved could be any size from a few MBs to 50MBs. I have logic that works on my Droid X 2.3.3 Razr 2.3.5 (I believe) but does not work on my Galaxy Nexus (has no removable storage but a built in 16Gig card with v4.0.2). I have looked around and haven't found any code/samples that work with v4.0. Maybe I am approaching this all wrong since it doesn't have an actual sd card? maybe it is something new in v4.0? Currently when I run my application on the Galaxy Nexus I get this: System.err(19520): java.io.FileNotFoundException:
UPDATED
InputStream inputStream = urlConnection.getInputStream();
File PATH = Environment.getExternalStorageDirectory();
File FILE = new File(Environment.getExternalStorageDirectory()+ "/" + FILENAME);
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
// buffer int bufferSize = 1024; int bufferLength = 0; byte[] buffer = new byte[bufferSize];
while ((bufferLength = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, bufferLength);
}
byte[] temp = byteBuffer.toByteArray();
FileOutputStream fos = new FileOutputStream(FILE);
fos.write(temp);
fos.close();
Are you putting this file in a specific directory on your sdcard?external storage?
I assume your permissions in your manifest are good because the 'permission denied is not raised' so maybe if you put the file in a specific folder, which is not created you should call the mkdirs() function on your file!
First, don't convert it to a string, just use getExternalStorageDirectory() as a File:
File sd = Environment.getExternalStorageDirectory();
File file = new File(sd, FILENAME);
I don't know if that will correct it or not, but it wouldn't hurt to try that. And you do not need to call file.createNewFile() before writing to the file with a FileOutputStream. The docs about FileOutputStream say:
An output stream that writes bytes to a file. If the output file
exists, it can be replaced or appended to. If it does not exist, a new
file will be created.
And which line of code is the FileNotFoundException happening on?

How to store downloaded images in to android SD card

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"

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