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.
Related
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
I'm trying to save the path of a downloaded file that was saved to the SD card, but I'm running into troubles when I try to retrieve it. When I try to convert it to a Bitmap, it says the file can't be found, even though it is on the SD card.
File example : /storage/emulated/0/_1404876264453.jpeg
Here's the code that I use to download a photo, and store it to the SD card (returns the URL of where it is saved on the device):
private String downloadProfile(String url)
{
String imageUrl = null;
try
{
String PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
String mimeType = getMimeType(url);
String fileExtension = "."+mimeType.replace("image/", "");
URL u = new URL(url);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();
long millis=System.currentTimeMillis();
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "_"+millis);
FileOutputStream f = new FileOutputStream(outputFile);
InputStream in = con.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read()) > 0){
f.write(buffer, 0, len1);
}
imageUrl = outputFile.getAbsolutePath()+fileExtension;
f.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return imageUrl;
}
After downloading it, I insert the above URL into a SQLite database; it stores fine. I used Astro file manager to check if the file was there, and it was.
Here's the code in my BaseAdapter that takes the above file url and attemps to convert it into a Bitmap:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
String profilePath = result_pos.get("profile_url"); // this just returns the example url
Bitmap bitmap = BitmapFactory.decodeFile(profilePath, options);
imageView.setImageBitmap(bitmap);
LogCat:
07-08 23:35:17.660: E/BitmapFactory(25463): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/_1404876264453.jpeg: open failed: ENOENT (No such file or directory)
check this link.i hope its useful to you.
reference link
1)create your own folder and save image name in proper way.
2)save image path in sqlite database
3)retrieve same path for get images.
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.
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();
}
In my android application, I need to upload a image in my Assets/Drawable/raw folder to the server.
I tried the following:
InputStream fileInputStream;
if(imageChanged) {
File file = New File("filename");
fileInputStream = new FileInputStream(file);
}else {
fileInputStream = ctx.getAssets().open("default.png");
}
int bytesAvailable;
byte[] buffer = new byte[102400];
while((bytesAvailable = fileInputStream.available()) > 0) {
int bufferSize = Math.min(bytesAvailable, 102400);
if(bufferSize<102400){
buffer = new byte[bufferSize];
}
int bytesRead = fileInputStream.read(buffer, 0,bufferSize);
dos.write(buffer, 0, bytesRead);
}
This executes fine. I am able to read the inputstream and write bytes to the DataOutputStream, the image is uploaded to the server.
Anyhow, the image at the server appears to be corrupted - only for the default image (uploaded in the 'else' block. The 'if' block image is not getting corrupted)
I also tried placing default.png in the 'raw' folder and tried the below
fileInputStream = ctx.getResources().openRawResource(R.drawable.default);
Same result here - the image at the server is corrupted.
I am starting to doubt if this is because the default.png is in the application space.
Can I get some help towards the proper way to upload an image in the application space (drawable/asset/raw)?
thanks!
nimi
It might have to do with the buffer size? I tried two different methods to read/write a png from the assets folder and both produced a working image. I used FileOutputStream to write to the sdcard but that should not be an issue.
InputStream is, is2;
FileOutputStream out = null, out2 = null;
try {
//method 1: compressing a Bitmap
is = v.getContext().getAssets().open("yes.png");
Bitmap bmp = BitmapFactory.decodeStream(is);
String filename = Environment.getExternalStorageDirectory().toString()+File.separator+"yes.png";
Log.d("BITMAP", filename);
out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
//method 2: Plain stream IO
String filename2 = Environment.getExternalStorageDirectory().toString()+File.separator+"yes2.png";
out2 = new FileOutputStream(filename2);
Log.d("BITMAP", filename2);
int r, i=0;
is2 = v.getContext().getAssets().open("yes.png");
while ((r = is2.read()) != -1) {
Log.d ("OUT - byte " + i, "Value: " + r);
out2.write(r);
i++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
if (out2 != null)
out2.close();
} catch (IOException e) {
e.printStackTrace();
}
}