I'm trying to save and then load the files that I've just saved but I can't find it.
I know it is being saved because I can look at the cache size under the Manage Application screen for the app and see that the size goes up when I save the image.
This is the error I'm getting: java.io.FileNotFoundException: /data/data/com.xxxxx/files/5ec2d71d-8a99-4258-a33a-91f6f99b8f0e.jpg
Here is my code:
imageDir = new File(context.getCacheDir().getAbsolutePath());
String newName = UUID.randomUUID().toString() + ".jpg";
Bitmap bmp = ImageLoader.getInstance().getBitmap(e.getUrl());
Boolean r = imageDir.exists();
Boolean c = imageDir.canWrite();
String[] d = imageDir.list();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(imageDir + "/" + newName));
bmp.compress(CompressFormat.JPEG, 90, out);
out.flush();
out.close();
d = imageDir.list();
FileInputStream fis = new FileInputStream(imageDir + "/" + newName);
ObjectInputStream ois = new ObjectInputStream(fis);
Bitmap b = (Bitmap) ois.readObject();
Ideas?
Context.openFileInput opens files within your app's data directory. What is the point of that call anyway? You already created a FileInputStream.
Related
I want to download the image from url and store it to internal memory after storing get the image stored in internal memory and display it in imageview
You can use this code to download the image
URL url = new URL(<your url>);
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = in.read(buf);
while (n!=-1)
{
out.write(buf, 0, n);
n=in.read(buf)
}
out.close();
in.close();
byte[] response = out.toByteArray();
And below code to save it to internal storage
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(response);
fos.close();
where file path for internal storage is
String filePath=getFilesDir().getPath() + File.separator + "image_" + <some unique identifier like int or string that is different for different images>
ANd to show in imageView use
File imgFile = new File(filePath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
Hope it helps.
I'm new to android and developing an app that saves large images from drawable folder to phone storage. These files have resolution of 2560x2560 and I want to save these files without loosing image quality.
I use following method to save images and it gives me Out of Memory Exception. I have seen many answers how to load a large bitmap efficiently. But I cant really find an answer for this problem.
In my code, I use
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageId);
File file = new File(root.getAbsolutePath() + "/Pictures/" + getResources().getString(R.string.app_name) + "/" + timeStamp + ".jpg");
file.createNewFile();
FileOutputStream oStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, oStream);
oStream.close();
bitmap.recycle();
Is there anything wrong with my code? This works without any exception for smaller images.
If I use android:largeHeap="true", this does not throw any exception. But I know it is not a good practice to use android:largeHeap="true".
Is there any efficient way to save large images from drawable folder without an exception?
Thank you in advance.
If you just want to copy the image file, you shouldn't decode it into a bitmap in the first place.
You can copy a raw resource file with this for example:
InputStream in = getResources().openRawResource(imageId);
String path = root.getAbsolutePath() + "/Pictures/" + getResources().getString(R.string.app_name) + "/" + timeStamp + ".jpg";
FileOutputStream out = new FileOutputStream(path);
try {
byte[] b = new byte[4096];
int len = 0;
while ((len = in.read(b)) > 0) {
out.write(b, 0, len);
}
}
finally {
in.close();
out.close();
}
Note that you have to store your image in the res/raw/ directory instead of res/drawable/.
I have an sqlite database which is written to from a service running on windows(C++). I am now trying to read from this same sqlite database which contains some blob data. I have some code as follows:
String tileQuery = "SELECT * FROM '" + layerName + "' WHERE zoom_level=?";
Cursor tileCursor = database.rawQuery(tileQuery, new String[] {zoom_level});
if( tileCursor.moveToFirst() )
{
while( !tileCursor.isAfterLast() )
{
int tileRow = tileCursor.getInt(tileCursor.getColumnIndex("tile_row"));
int tileColumn = tileCursor.getInt(tileCursor.getColumnIndex("tile_column"));
byte[] tileData = tileCursor.getBlob(tileCursor.getColumnIndex("tile_data"));
//Write tile to file
String fileName = layerName + "_" + zoom_level + "_" + tileRow + "_" + tileColumn + ".jpeg";
try {
/*
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + TILE_STORAGE_PATH + "/" + fileName));
bos.write(tileData);
bos.flush();
bos.close();
*/
ByteBuffer bb = ByteBuffer.wrap(tileData);
bb.order(ByteOrder.LITTLE_ENDIAN);
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + TILE_STORAGE_PATH + "/" + fileName);
byte[] toWrite = new byte[bb.remaining()];
bb.get(toWrite, 0 , toWrite.length);
fos.write(toWrite);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
tileCursor.moveToNext();
}
}
As shown, I am attempting to write the blobs to disk as jpeg images. No matter what I do, the images appear to be corrupt, as in I cannot view them on any image viewer within android. The same images can be written to file on windows and viewed correctly, which made me think that it was an endianess issue(due to the fact that the blob was written to the database via a service running on windows). I have tried changing the byte order and writing to disk again, but I get the same result. Could anyone suggest what I might be doing wrong/missing. Any help is greatly appreciated.
To make this work there are a few different steps. Assuming your database connection is working and those are the correct columns you are looking in with your Cursor
(1) Convert the blob to a Bitmap. You can use the blob you get back, assuming you actually downloaded and stored it to your local database, as the byte[] you will decode.
Bitmap bm = BitmapFactory.decodeByteArray(tileData, 0 ,tileData.length);
(2) Create a new file in the approprite directory and write to that file. You can do that with something like the code below. The idea is to get the local directory
private void storeBitmap(Bitmap myBitmap){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/your_directory_name");
String fname = "your_file_name.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
If you want to add the images to gallery or you just want a different (and potentially easier) way to add the file, look into using MediaScanner which will add the files as though you took the picture with your camers
Need to display images downloaded and extracted from a zip into the "/files" directory of the app. the images are getting in there properly as far as I can tell - I am able to extract them from the emulator and view/open them from my desktop. but every attempt, every variation of code I have found and tried so far has failed (Tag: skia / Text: --- decoder->decode returned false).
My latest construct, which does work for image files downloaded separately and uncompressed :
String imgFile = new File(getFilesDir(), "myImage.jpg").getAbsolutePath();
ImageView myImageView = new ImageView(this);
Bitmap bm = null;
try{
bm = BitmapFactory.decodeFile(imgFile);
myImageView.setImageBitmap(bm);
} finally{
mainLayout.addView(myImageView);
}
And here is the construct I am using to handle the zip extraction. I assume this is where the problem lies but I am clueless as to what I could possibly do differently and to what effect:
ZipInputStream zis = new ZipInputStream(fis);
BufferedInputStream in = new BufferedInputStream(zis, 8192);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null){
File dest_file = new File(getFilesDir(), ze.getName());
FileOutputStream fout = new FileOutputStream(getFilesDir() + "/" + ze.getName());
BufferedOutputStream out = new BufferedOutputStream(fout, 8192);
byte b[] = new byte[1024];
int n;
while ((n = in.read(b,0,1024)) >= 0) {
out.write(b,0,n);
}
for (int c = zis.read(); c != -1; c = zis.read()) {
fout.write(c);
}
zis.closeEntry();
fout.close();
}
zis.close();
fis.close();
At a terrible standstill here. Appreciate any solutions/suggestions.
I'm trying to save an image to my sdcard but running into the following error:
java.io.IOException: Parent directory of file does not exist: /sdcard/skdyImages/a46e2e08-9154-4fe7-96e8-2af0a7a92867.jpg
I do have the permissions in my manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Here is my code:
String newName = UUID.randomUUID().toString();
Bitmap bmp = ImageLoader.getInstance().getBitmap(e.getUrl());
File file = new File("/sdcard/skdyImages", newName + ".jpg");
file.getParentFile().mkdirs();
file.createNewFile();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
bmp.compress(CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Any ideas?
Try something like this...
// Automatically creates folder on sdcard called /Android/data/<package>/files
// if it doesn't exist
File ImageDir = new File(getExternalFilesDir(null).getAbsolutePath());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(ImageDir + "/" + newName + ".jpg"));