Im downloading lots of .gif files from a server and when I try to draw on them in a canvas it never works. Ive created the bitmaps from the gifs and have set them to be immutable using the Options class and have tried just creating a new file and saving the image as a png but that doesnt seem to work either. Does somebody happen to have a good simple way to convert a gif to a png? So far Ive tried:
Bitmap b = BitmapFactory.decodeFile(mediaStorageDir.getAbsolutePath() + "/" +
cr.getString(cr.getColumnIndex(gh.Zone_ZoneName))+".gif");
OutputStream stream = null;
File f = new File(mediaStorageDir.getAbsolutePath() + "/" +
cr.getString(cr.getColumnIndex(gh.Zone_ZoneName))+".png");
if(f.createNewFile()){
System.out.println("PNG CREATED "+f.getAbsolutePath());
}else{
System.out.println("PNG NOT CREATED "+f.getAbsolutePath());
}
stream = new FileOutputStream(f.getAbsolutePath());
b.compress(CompressFormat.PNG, 100, stream);
stream.close();
But this doesnt seem to work. I never even see the system.out.println calls either and I know that the code is being run because right above the gif's are being saved.
I figured out my problem. There was a system lock on the newly created file when I was trying to access it to make a png from it. When I moved the call elsewhere to make the png it worked fine.
Related
I am trying to make an app which can generate an image file(JPEG/PNG) with dimensions equal to the user's phone's screen size and fill it with a single solid color(BLACK/BLUE/GRAY, etc). After generating it, it should save the image to the external storage to be used in any other app.
Any help would be appreciated.
So far I have been able to do this based on a few answers but it doesn't generate any image file. (I have given the permission to write in External Storage)
Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.ARGB_8888);//MUTABLE bitmap
File file = new File(Environment.getExternalStorageDirectory(),"/image" + System.currentTimeMillis() + ".png");
FileOutputStream str;
try {
str = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG,100,str);
}
catch (IOException e) {
e.printStackTrace();
}
This was just an attempt to check whether any image would be generated/stored or not.
try closing the stream: str.close() after you write to it
EDIT: since it worked, let me explain a bit: the stream keeps everything buffered in memory until some time (this isn't a totally deterministic thing), so if you close the stream it flushes everything it has buffered. So it's always a good practice to close streams.
I want to generate a qr code image and add it programmatically to the assets drawable folder of the app.
In the mean time, you would add it mannually in eclipse or android studio. Just wonder is there any ways to do it programmatically as well.
Many thanks!
This is simply not possible, you cant't modify/add that folder once you have generated apk and installed app. What you can do is to generate a folder on internal or external storage and save your images there.
It is already disccussed here and here
Asset Folder is used to load our Data with application , it never be changed at run time , AssetManger has method to read Asset Data and there is no way to write within Asset programmatically at Run Time.
Rather if you want to store your data at run time , You may store in Internal Memory like below Code.
Drawable drawable = getResources().getDrawable(R.drawable.demo_img);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytearrayoutputstream);
file = new File( Environment.getExternalStorageDirectory() + "/SampleImage.png");
try
{
file.createNewFile();
fileoutputstream = new FileOutputStream(file);
fileoutputstream.write(bytearrayoutputstream.toByteArray());
fileoutputstream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
After searching about "How to save Layout views as images", I've found some solution to save in Internal and External Storage. But It seems the image file created is going to save in some data/data/... folder that is not visible normally. Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not:
View content = findViewById(R.id.relativeLayout);
String yourimagename = "MyImageFile";
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/" + yourimagename + ".png");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
ostream.close();
content.invalidate();
} catch (Exception e) {
e.printStackTrace();
} finally {
content.setDrawingCacheEnabled(false);
}
But It seems the image file created is going to save in some data/data/... folder that is not visible normally.
The file will be saved where the programmer elects to save it.
Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not
That code will not work on any version of Android, as new File("/" + yourimagename + ".png") is not going to give you a usable File, as it points to a place that you can neither read nor write.
You are welcome to save the image to internal storage or external storage. Since you want this image to be picked up by "gallery"-type apps, you are best off choosing external storage, then using MediaScannerConnection and its scanFile() method to get the file indexed by the MediaStore, since gallery apps will tend to use the MediaStore as their source of images.
On the whole, I worry that getDrawingCache() will be unreliable. You may be better served telling your root View to draw to your own Bitmap-backed Canvas instead.
I have a problem when saving image from web.
I create new folder and save all images to this. The problem is I want this folder order by the last download image on first but When i open gallery, the folder is order by the image date created and the date created in the downloaded image is not the time I download but the first time it created. I've already search on stack over flow and see that java can't modified the date created in image to the time I download it.
Any one has the solution? (Sorry for the bad English)
Thanks you for the comment. I will explain more details.
First, I download image from web to cache directory
HttpURLConnection localHttpURLConnection = (HttpURLConnection) new java.net.URL(urldisplay).openConnection();
localHttpURLConnection.setConnectTimeout(30000);
localHttpURLConnection.setReadTimeout(30000);
localHttpURLConnection.setInstanceFollowRedirects(true);
InputStream in = localHttpURLConnection.getInputStream();
File localFile = Constans.fileCache.getCacheFile(urldisplay);
FileOutputStream fos = new FileOutputStream(localFile);
Utils.CopyStream(in, fos); // simple copy by trunks
fos.close();
Second, I copy downloaded image to external storage
File toFile = new File(Environment.getExternalStorageDirectory() + "/folder", "folder_" + System.currentTimeMillis() + ".png");
FileOutputStream fos = new FileOutputStream(toFile);
Utils.CopyStream(new FileInputStream(fromFile), fos);
fos.close();
// Scan image to display when open with gallery otherwise it couldn't see in gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(toFie);
mediaScanIntent.setData(contentUri);
mContext.sendBroadcast(mediaScanIntent);
Lastly, I see that gallery don't sort my image by the time I downloaded. That is the problem i want to fix.
Not sure I understood, but let's try.
First the issue you mention is more specific to Gallery application than a java code issue.
I assume Gallery use EXIF information to order the picture by date they were taken, not oder they are downloaded/copied. Unfortunatly Gallery does not provide any option to sort picture in other oders.
Maybe you can try to use another explorer that allows you to sort the pictures in another order (maybe ESFileExplore which has more options?)
Ultimate solution: you can try to change EXIF in your pictures using a java EXIF library to modify picture taken date and this should change the order they appear in the Galery (but very ugly solution...). Some random EXIF libraries after 5 seconds of Google:
http://drewnoakes.com/code/exif/
http://www.toanthang.net/modules.php?name=News&new_topic=2&catid=7
Hope this helps
Thierry
I have this code to take screenshots of layouts in Android. It is not throwing any errors, however, the screenshot is not being taken either. Can someone please help me figure out what I am doing wrong here? I am new with Eclipse and I am having a hard time figuring things out. Also if there is any other way to take screenshots can you post it as an answer to this thread? Thanks for your time!
private void getScreenshot()
{
View content = findViewById(R.id.testView);
content.setDrawingCacheEnabled(true);
content.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());
content.setDrawingCacheEnabled(false);
File file = new File( Environment.getExternalStorageDirectory() + "image.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
Do you need to add path separator into your File? i.e.
File file = new File(Environment.getExternalStorageDirectory() +
File.separator + "image.png");
You should add a lot more logs and tests in your code to check whether it is behaving as you would expect, e.g.
Log the details of the file you are trying to create to be sure it is correct.
Once you've created the file, test that it exists, e.g. if (!file.exists())
The Bitmap.compress function returns a boolean, so you should check the return value and log it to see if it succeeded.
One other thought: maybe you need to call ostream.flush() (API docs here) to ensure the buffered data is written to the file?
I'm assuming you're writing this code for use within your app. You probably already know this, but DDMS provides a way to take screenshots in case you just want to take some yourself. Just make sure to select the device to enable the Screenshot menu option.