I want to read .gif file from internal storage. I can read image, mp3 but not .gif. Actually I am searching global way to read any types of file.
Regards
Edit:
private void globalGif() throws FileNotFoundException{
FileInputStream fis = openFileInput("frog");
GifMovieView gmv = new GifMovieView(getApplicationContext(), fis);
setContentView(gmv);
}
I use this code but it shows this error. divide by zero fis haven't gif file.
If you want to read gif files, take a look at the Movie class.
Here's a nice tutorial which show you how to load a gif and show it.
you can find the path of image following way
File file= new File(Environment.getExternalStorageDirectory()
+ "/Images/a.gif");
But you can not set this gif image to ImageView the gif file is show in webView.
Related
I'm trying to load an svg file downloaded into sd card with Picasso but it doesn't work.
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+"/secciones.svg");
Picasso.with(context).load(file).into(holder.ivIcon);
I found this question before but he load the file from assets.
Load a vector drawable into imageview from sd card
Is it possible to load the .svg downloaded into the imageView?
You may achieve this by converting SVG to drawable before setting it to ImageView. For this purpose there is a nice library (a bit old and un-managed) Here, however I tried and worked for me. The code is as below :
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "your_file_path/filename.svg");
try {
FileInputStream fileInputStream = new FileInputStream(yourFile);
SVG svg = SVGParser.getSVGFromInputStream(fileInputStream);
Drawable drawable = svg.createPictureDrawable();
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
imageView.setImageDrawable(drawable);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
We cannot use SVG with android application. (yes…there are 3rd party libraries that we can use to work with SVG files)
Source : Android — Working with VectorDrawable
VectorDrawable images are simply xml file that contains all information of an image (like lines, curves etc. ) and the color associated with each one of them. The biggest advantage of using vectorDrawable is that we need only one image for different screen devices.This not only reduces the size of the final apk, also it simplifies the maintenance of the project.
I am working on an android application where I want users to upload a file and I want to read the contents of uploaded file and display it . I have been reading files from SD card but I now I need the user to upload a file . I searched a lot but I didn't get any solution for it.
My code for reading the file from SD card
File file = new File(dir, "/tounzip/b.txt");
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
DataInputStream input = new DataInputStream(fileInputStream);
Its reading the file contents successfully but Is there any way to make the user upload the file and read the contents from it ? Any help would be great !! Thanks !!
Android file chooser
You require a file picker/file chooser.
Logic is you read the stream and save the stream in your app sandbox (app's memory i.e. /data/package..) then do whatever you want from there.
If file size is small then even a in-memory implementation will help.
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
My webview saves lot of cache on SD Card when I visit a site which contains images and they don't show their format. When I try to open them, the default Android file manager says "File type not found". Then I changed the format of some of them(mostly the big size one) to .jpg and bingo, they are the images I saw on the webpage.
Punchline: I want to open the images from that sites that are saved as cache, in another activity, would I have to go on and rename all the cache files to .jpg then call them or there is another way to do it.
Thank you.
You can use the BitmapFactory to open these images files. You can try:
- decodeFile(String pathName)
If it gives some problem with the extension, try decodeStream (InputStream is), it receive an InputStream and it should work.
You can list a folder with File:
File f = new File(Environment
.getExternalStorageDirectory()
.getAbsolutePath());
File[] files = f.listFiles();
for (File file : files){
//Do something with this file
}
Actually i know how to open PNG files as bitmaps. But my code doens't works for open JPG files, i dont know why.
I can't find correct examples on SO or google about how to do this.
I need to have a bitmap with the JPG file opened from a dir of the sdcard. For example "sdcard/images/01.jpg"
Thanks
File root = Environment.getExternalStorageDirectory();
ImageView IV = (ImageView) findViewById(R.id."image view");
Bitmap bMap = BitmapFactory.decodeFile(root+"/images/01.jpg");
IV.setImageBitmap(bMap);
Always try to use Environment.getExternalStorageDirectory(); instead of sdcard.
You need an ImageView somewhere in your layout, however that's how I do this kind of things.
I use this code personally too, and it works here.
Any of the BitmapFactory.decode* methods should be able to handle standard JPG files.
If you post some code it could be easier to see why it won't work.