Android Picasso - Save image locally before viewing it in ImageView - android

I have made an app which shows a list of image. But before, I downloaded all the images to the sdcard, and I show a progress dialog with the number of downloaded files. I'm thinking to optimise the process using Picasso because of many images to download. So no progress dialog. With Picasso, I would like to save the image in my app directory (as a normal downloaded file) before it is shown in the ImageView. I thought to use this code :
Picasso picassoInstance = new Picasso.Builder(context)
.downloader(new CustomDownloader(context, destination))
.build();
The CustomDownloader :
public class CustomDownloader extends OkHttpDownloader {
private File destination;
public CustomDownloader(Context context, File destination){
super(context);
this.destination = destination;
}
#Override
public Response load(Uri uri, boolean localCacheOnly) throws IOException {
Response response = super.load(uri, networkPolicy);
FileOutputStream fileOutput = new FileOutputStream(destination);
InputStream inputStream = response.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
return response;
}
}
And finally, I use this statement to load the image in the ImageView :
picassoInstance.load(item.getItem().getPath()).resize(width,0).into(imageView);
Am I correct to use this kind of implementation ? Or do you can provide another way ?
EDIT 1 : I use the code above but the image is not shown in the ImageView, even if I see the images downloaded in the SDCARD.
EDIT 2 : With this code, using new Thread :
#Override
public Response load(Uri uri, boolean localCacheOnly) throws IOException {
Response response = super.load(uri, networkPolicy);
new Thread(new Runnable() {
try {
FileOutputStream fileOutput = new FileOutputStream(destination);
InputStream inputStream = response.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
} catch(IOException e){
e.printStackTrace();
}
}).start();
return response;
}
I got an java.lang.ArrayIndexOutOfBoundsException: src.length=2048 srcPos=2048 dst.length=1024 dstPos=0 length=1024 in this line while ((bufferLength = inputStream.read(buffer)) > 0)

using picasso to load image is good, there are a serveral advantage like:
Memory and disk caching of uncompress imagery, post-processing
An automatically created singleton image downloader
Multiple downloads at one time
But you can try to use Volley, for me this is the best!
About your problem, try to show item.getItem().getPath() in logcat and look if the path is ok or not, the path should be the path in sdcard if you want to use the image in sdcard

Related

Android - Load image byte by byte in imageview

I have a large size of the image file which is nearly 16MB size. I want to load this image in my imageView and zoom it after that add markers. I tried this with subsampling-scale-image-view. I am following the below link https://github.com/davemorrissey/subsampling-scale-image-view .
The important point is I am loading image from url. The above library not supporting that. So I just downloaded the image and save to SD card after that load from that local file. Technically which is working.
Issue:
Now the issue is it is taking too much of time for the first time downloading. Also even second time it takes nearly a minute.
My Idea:
Due to this issue, I try to load image byte by byte. Once the image is downloading 100bytes then show that in imageView next download the next part of the image from url. Is it possible to doing like that?
Currently I am loading image like the following code:
URL url = new URL(url_);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream(root+"/"+ fileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
runOnUiThread(new Runnable() {
#Override
public void run() {
image.setImage(ImageSource.uri(root+"/"+ fileName));
}
});
Can somebody help me to solve this riddle?
Note: If there are any possibilities other than this library pls add your suggestions.
Never tried this but you can check if this work.
Get the data from url in form of byte Array.
data = getImageStream(url); //should call in async Task..
Now convert byte array into bitmap and set in imageView.
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
image.setImageBitmap(bitmap)
Not writing into file.This will help with some performance improvement.
public byte[] getImageStream(String url){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
is = url.openStream ();
byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
int n;
while ( (n = is.read(byteChunk)) > 0 ) {
baos.write(byteChunk, 0, n);
}
}
catch (IOException e) {
System.err.printf ("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
e.printStackTrace ();
// Perform any other exception handling that's appropriate.
}
finally {
if (is != null) { is.close(); }
}
return baos.toByteArray();
}

How to download all images one by one and show progress status

I want to download all the images I have on server in the array string of Url one by one so that I may do not have to download a zip file of images from the server and to unzip after downloading it.
So I thought to download the images one by one and to show the download status in the progress bar. But I am extremely failed in it. An Idea came into my mind to make the string array of the Url and to use the For loop to download but it is downloading the last image of the String array and decline or pass all other images in the array . I think I have got the idea that what is going on but I have know Idea what would be the solution then.
What I have done So far
protected Void doInBackground(Void... arg0) {
try {
//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
String [] imageUrl = {"http://www.androidbegin.com/tutorial/flag/india.png","http://www.androidbegin.com/tutorial/flag/pakistan.png"
,"http://www.androidbegin.com/tutorial/flag/china.png","http://www.androidbegin.com/tutorial/flag/unitedstates.png"};
URL url;
HttpURLConnection urlConnection = null;
for(int i=0;i<imageUrl.length;i++){
url = new URL(imageUrl[i]);
//create the new connection
urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
}
File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Test");
storagePath.mkdirs();
String finalName = Long.toString(System.currentTimeMillis());
File myImage = new File(storagePath, finalName + ".png");
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(myImage);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ((bufferLength = inputStream.read(buffer)) > 0) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//this is where you would do something to report the prgress, like this maybe
// updateProgress(downloadedSize, totalSize);
}
//close the output stream when done
fileOutput.close();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// see http://androidsnippets.com/download-an-http-file-to-sdcard-with-progress-notification
return null;
}
** What I want :**
Download all the images one by one.
After downloading one Image it should get save in the device and update the progress status.
Please show me some source code rather then giving me just Idea how to do it. And little source code and complete work around on this would be appreciated.
the saving image code should be taken inside for loop. as this code is outside of for loop only your last image is getting saved as at the end of for loop last url is used.

Can't save images from URL. Android

first, sorry for my bad english and second, I have a "little" problem.
I tested a lot of codes from StackOverFlow but i continue with the same problem.
I'm trying to download some images from URL. I have an ExpandableListView and I use a class named Downloadusers to download all information about users.
In this class I get the user's photo URL and I download the images with the following code:
private void downloadFile(String url) {
String filepath = null;
try
{
URL nurl = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) nurl.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = getExternalFilesDir(null);
String filename = url.substring(url.lastIndexOf('/') + 1);
Log.i("Local filename:",""+filename+" SDCardRoot: "+SDCardRoot.toString());
File file = new File(SDCardRoot,filename);
if(file.createNewFile())
{
file.createNewFile();
}
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[PHOTO_FILE_MAX_SIZE];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 )
{
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
}
fileOutput.close();
if(downloadedSize==totalSize) filepath=file.getPath();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
filepath=null;
e.printStackTrace();
}
Log.i("filepath:"," "+filepath);
}
I have also verified that the URLs are correct and I have loaded in the browser.
With Log.i("filepath:"," "+filepath); I can see that filepath is correct, so I think that images are downloaded correctly but no, image files are corrupt files, so when I go to load the images into my ImageView I have a NullPointException because bMap readed is null due to corrupt images.
I have all permissions: Internet, Write and read external storage and phone state.
I tried too download images with AsyncTask, but I have the same problem.
Someone know what can be my problem?
Thanks.
Here it is my download method. You will download image into SDCARD. You can check whether image is downloaded or not by going DDMS Perspective.
public void download(String Url) throws IOException {
URL url = new URL (Url);
InputStream input = url.openStream();
try {
File storagePath = new File(Environment.getExternalStorageDirectory());
OutputStream output = new FileOutputStream (new File(storagePath, 'myImage.jpg'));
try {
byte[] buffer = new byte[2048];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
}
This snippet is for showing downloaded image in ImageView.
ImageView image = (ImageView) findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeFile(Environment
.getExternalStorageDirectory() + "myImage.jpg");
image.setImageBitmap(bitmap);
I solved my problem using library nostra13 "universal-image-loader-1.9.2.jar". My code is now:
// If the user photo exists and is public, download and show it.
if (Utils.connectionAvailable(activity)
&& (photoFileName != null) && !photoFileName.equals("")
&& !photoFileName.equals(Constants.NULL_VALUE)) {
// Create options. Setting caché = true (default = false)
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.build();
// Create global configuration and initialize ImageLoader
// with this configuration
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(activity.getApplicationContext())
.defaultDisplayImageOptions(options)
.build();
ImageLoader.getInstance().init(config);
// Load image, decode it to Bitmap and display Bitmap in ImageView
// (or any other view
// which implements ImageAware interface)
ImageLoader.getInstance().displayImage(photoFileName, image);
}
With that code I can load the image on caché and show in my imageview without problems.
Thanks to all.
I had the same problem, but I was able to solve it by setting
urlConnection.setDoOutput(false);
and it worked, but I don't know why.

Is it possible to convert pdf files to images in android

I would like to convert a pdf file to images in android. While I tried to convert with java it works perfectly, in android it does't accepting buffered class.
I read So many open source files iText, PDFReader and PDfViewer, But none doing the job perfectly.Can any one help me in that part?
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.*;
public class ImageMain {
// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels; for this method's
// implementation, see e661 Determining If an Image Has Transparent
// Pixels
boolean hasAlpha = hasAlpha(image);
// Create a buffered image with a format that's compatible with the
// screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha) {
transparency = Transparency.BITMASK;
}
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha) {
type = BufferedImage.TYPE_INT_ARGB;
}
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
public static void setup() throws IOException {
File file = new File("p.pdf");
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
int numPgs = pdffile.getNumPages();
for (int i = 0; i < numPgs; i++) {
// draw the first page to an image
PDFPage page = pdffile.getPage(i);
// get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
// generate the image
Image img = page.getImage(rect.width, rect.height, // width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
// save it as a file
BufferedImage bImg = toBufferedImage(img);
File yourImageFile = new File("page_" + i + ".png");
ImageIO.write(bImg, "png", yourImageFile);
}
}
public static boolean hasAlpha(Image image) {
// If buffered image, the color model is readily available
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage) image;
return bimage.getColorModel().hasAlpha();
}
// Use a pixel grabber to retrieve the image's color model;
// grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}
// Get the image's color model
ColorModel cm = pg.getColorModel();
return cm.hasAlpha();
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
ImageMain.setup();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}
JAR FILE USED: PDF RENDERER.jar.
This Java code can convert pdf pages to images.But while i tried to convert as android code . I got error that java.awt.Rectangle and java.awt.graphics not supporting.
I searched in net i got a jtar.jar that just removes the error and makes to run the projext but when running the exception says Rectangle and Graphics not supporting..What mistake i made friends..? Any Ideas..?
I found it..I used ghostscript in my webserver and converted to images then store in webfolder.
refer this to convert using ghostscript
http://view.samurajdata.se/
Then you have to count the files.
Then i retrieved my images to my app using URL with the help of for loop and displayed in an bitmap format..
The code i used to retrieve from url:
public String Downloadfromurl(String Url)
{
String filepath=null;
try {
URL url = new URL(Url);
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
folder = new File(Environment.getExternalStorageDirectory().toString()+"/img");
folder.mkdirs();
//create a new file, specifying the path, and the filename
//which we want to save the file as.
String filename= "page"+no+".PNG";
file = new File(folder,filename);
if(file.createNewFile())
{
file.createNewFile();
}
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//this is where you would do something to report the prgress, like this maybe
Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
}
//close the output stream when done
fileOutput.close();
if(downloadedSize==totalSize)
filepath=file.getPath();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
filepath=null;
e.printStackTrace();
}
Log.i("filepath:"," "+filepath) ;
return filepath;
}
I hope this might be useful.

Get image from web and save it to phones memory ?

I need to get image from the web and store it in the phone for later use.
I tryed this:
public Drawable grabImageFromUrl(String url) throws Exception
{
return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
}
So this my function to grab image from Url, i just need a proccess to get the returned drawable and save.
How can i do that ?
see this complete example give here
http://android-example-code.blogspot.in/p/download-store-and-read-images-from.html
Based off here, you can actually download the image using a different method. Is it absolutely necessary that you store it as a drawable before saving it? Because I think you could save it first, and THEN open it, if need be.
URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();
try {
//The sdcard directory e.g. '/sdcard' can be used directly, or
//more safely abstracted with getExternalStorageDirectory()
String storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (storagePath + "/myImage.png");
try {
byte[] buffer = new byte[aReasonableSize];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}

Categories

Resources