This is perhaps a very naive question, but I cannot figure it out.
After writing to a file with FileOutputStream, I read the file again to get updated content, but only get the original content:
File avatarFile = new File(avatarFilePath);
if(avatarFile!=null){
if(!avatarFile.exists())
avatarFile.createNewFile();
fos = new FileOutputStream(avatarFile);
}
boolean result = scaledBitmap.compress(Bitmap.CompressFormat.JPEG,Constants.COMPRESSED_AVATAR_QUALITY, fos);
fos.flush();
fos.close();
//Now read again, but get the old avatar
File newfile = new File(avatarFilePath);
Picasso.with(context).load(newfile).into(imageView);
What is happening here?
Picasso is using the old cached image.
To bypass the memory cache, use following:
Picasso.with(context).load(newfile).skipMemoryCache().into(imageView);
Related
i want to save a svg file from web to a file and then show it from file. i use this code to save a png file :
OutputStream fos = null;
File file = new File(getApplicationContext().getCacheDir(),FilenameUtils.getBaseName(url.toString())+FilenameUtils.getExtension(url.toString()));
Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bm.compress(Bitmap.CompressFormat.PNG, 50, bos);
bos.flush();
bos.close();
what should i do for svg file ?
In theory, you should be able to do something like the following:
PictureDrawable pd = (PictureDrawable) imageView.getPicture();
Picture picture = pd.getPicture();
picture.writeToStream(os);
However you should not do this. writeToStream() is deprecated (as is createFromStream()). I presume the reason is that the format of a Picture may change in the future and any saved pictures may no longer load. If you are just using it for temporary caching while the app is running, then that may be okay.
But it would be better, as #greenapps says, to cache the original SVGs.
In my Android project I am trying to save a bitmap to the SD card at a given location.
When I run this, I get an IOException every time, saying permission denied. This is on creating the FileOutputStream.
This leads me to believe that I am missing permissions, but I include READ and WRITE_EXTERNAL_STORAGE. They are declared before my uses-sdk block and are outside the application block. I also read/write text to a file elsewhere in my application and that works just fine. My current code was modified to match several of the solutions I found online, but I have not been able to successfully save a bitmap file.
If anyone could point out my error I would be extremely thankful.
ImageView imageView = (ImageView) findViewById(R.id.new_pnm_pic);
Bitmap bm = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(bm);
String outPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Recruits";
try {
File d = new File(outPath);
if (!d.exists())
d.mkdirs();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd-kk-mm-ss");
String currentTime = sd.format(new Date());
File file = new File(outPath+'/'+currentTime+".jpg");
FileOutputStream fOut = new FileOutputStream(file.getPath());
bm.compress(Bitmap.CompressFormat.JPEG, 50, fOut);
fOut.flush();
fOut.close();
}
catch(IOException ioe){
Toast.makeText(this, "Nice try but didn't work", Toast.LENGTH_SHORT).show();
}
Change This
File file = new File(outPath+'/'+currentTime+".jpg");
To
File file = new File(d+'/'+currentTime+".jpg");
And Add Permission to Manifest File:-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Edit
Before we start saving the image to sd card ,we need to check whether the sd card is mounted or not.
I'm writing to a file using the code below:
File file = new File(getCacheDir(), "cachefile");
FileOutputStream fos = new FileOutputStream(file);
StringBuilder cachetext = new StringBuilder();
Iterator bri = brands.iterator();
Iterator bli = brand_id.iterator();
while(bli.hasNext()) {
cachetext.append(bli.next() + "|" + bri.next() + System.getProperty("line.separator"));
}
fos.write(cachetext.toString().getBytes());
fos.close();
This works fine - no errors and the file ends up containing what I expect it to contain. When I go to read it via openFileInput(), however, I get an exception telling me that path separators are not allowed
FileInputStream fis = openFileInput(getCacheDir() + "/cachefile");
Fair enough, that contains a slash, but how else can I specify the path of the file I want to open? There must be a way to do this, of course, but I can't find answers via Google ('read', 'cache' and 'file' not being the most niche of terms ...) so I thought I'd try the human touch. Thanks in advance!
you do it pretty much the same way you created the output file:
FileInputStream fis = new FileInputStream(new File(getCacheDir(), "cachefile"));
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"
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!