FTPClient in android convert image to bitmap - android

Help!! i have installed ApacheCommons.net FTPClient. As i need to download a PNG file via ftp. The stream downloads but iam having trouble converting it into a Bitmap so i can save to local storage.
mFTPClient.connect("path");
mFTPClient.login("anonymous","nobody");
mFTPClient.enterLocalPassiveMode();
mFTPClient.changeWorkingDirectory("/fax");
InputStream inStream = mFTPClient.retrieveFileStream("nweimage.PNG");
InputStreamReader isr = new InputStreamReader(inStream, "UTF8");
Been trying to use BitmapFactory to convert it, but just keep getting null return.
Any pointers
Cheers

This is how i got around it. Thanks greenapps for making me think down a different route
mFTPClient.connect("path");
mFTPClient.login("anonymous","nobody");
mFTPClient.enterLocalPassiveMode();
mFTPClient.changeWorkingDirectory("/fax");
mFTPClient.setControlEncoding("UTF-8");
mFTPClient.setFileType(FTPClient.BINARY_FILE_TYPE);
try{
FileOutputStream out = new FileOutputStream(file);
boolean status = mFTPClient.retrieveFile("image.PNG", out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

Related

Save file object to internal storage as image

I am downloading image via a URL using loopj library https://github.com/loopj/android-async-http. In response I get image as File object. I want to store this image as .jpg to my internal storage and get the path of that image. Can anyone please help me regarding this? Or suggest me any other library or the code through which I can achieve this functionality?
Try :
try {
File imgFile = null; //File you received from loopj
FileInputStream fis = new FileInputStream(imgFile);
FileOutputStream fos = new FileOutputStream(
new File("yourPath.jpg"));
byte fileContent[] = new byte[(int) imgFile.length()];
fis.read(fileContent);
fos.write(fileContent);
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}

How to create a file from google glass application?

I have an application that runs on glass. I want my application to create files at run time and be able to write/read data to/from those files. Can anyone show me a way to do this? Does Android's openFileOutput() work in glass?
(In case if anyone else has the same question)
Okay I figured out a way to do this. It looks like Java i/o libraries works fine with android and also for glass. The following works fine in glass.
String filename = "sensorData";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_APPEND);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter (outputStream);
outputStreamWriter.write(content);
outputStreamWriter.close();
}
catch (Exception e) {
e.printStackTrace();
}
//Printing the file in logcat just to verify the contents
FileInputStream inputStream;
try
{
inputStream = openFileInput(filename);
InputStreamReader inputStreamReader = new InputStreamReader (inputStream);
char[] buffer = new char[content.length()];
inputStreamReader.read(buffer);
String input = buffer.toString();
Log.i(input);
}
catch (Exception e)
{
e.printStackTrace();
}

Android: write bytes to image file on SD Card

I'm trying to create an image file on sd-card, building it from the bytes that a server is sending towards me after calling a web-service (basically: download file).
I managed to get "something" on the client-side, and try to write those bytes to a file, using:
FileOutputStream fOut = null;
BufferedOutputStream bOs = null;
try {
fOut = new FileOutputStream(returnedFile);
bOs = new BufferedOutputStream(fOut);
bOs.write(bytesToWrite);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (bOs != null) {
bOs.close();
fOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
but the image file is broken (its size is > 0kb, but broken).
I ended up opening that file on my computer with a text editor, and I saw that some of the initial file data (before being sent), differs from the final one. So I'm guessing that there is some kind of encoding misstakening or something like that.
I would appreciate an idea of how to make this work ( download image file from a web server, and open in on my phone).
PS. I can also change or get info about the server configuration, as it was configured by a friend of mine.
PS2. I should be able not to download only images, but any kind of file.
I think It's better you encode the image to Base64 in your server, for example in PHP you can do it like this :
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
And then in android you decode the Base64 string into your image file:
FileOutputStream fos = null;
try {
if (base64ImageData != null) {
fos = context.openFileOutput("imageName.png", Context.MODE_PRIVATE);
byte[] decodedString = android.util.Base64.decode(base64ImageData, android.util.Base64.DEFAULT);
fos.write(decodedString);
fos.flush();
fos.close();
}
} catch (Exception e) {
} finally {
if (fos != null) {
fos = null;
}
}
First of all make sure you have this permissions on your android manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
File stream is designed to work on local file storage rather than network connection. Use URLConnection class instead.
URL uri = new URL("Your Image URL");
URLConnection connection = uri.openConnection();
InputStream stream = connection.getInputStream();
//DO other stuff....

How can I convert response json string image into a displayable image

My final year project requires me to develop a mobile application which fetches a number of Json values from a server. This is my first experience in android development but sure learned something’s and enjoyed the experience.
I have managed to develop the following.
1-a json parser class which fetches the value.
2-a class which displays these values.
3-a database where I store the json response.
However I'm one step away from completing my project, I cannot display the response string image address as real images (shame on me).
I need to do the following.
1- Parse the string path of the image and display the response as image.
I have spent most of my time trying to find the solution on the web, stack overflow. But no luck so far.
I need to do something like this in order to display these images together with the text descriptions.
I have now reached the cross roads and my knowledge has been tested.Is what i'm trying to do here posible?.
Who can show me the way? ,to this outstanding platform.
if you have the image url as a string, you can load and save the image using something like this:
try {
Bitmap bitmap = null;
File f = new File(filename);
InputStream inputStream = new URL(url).openStream();
OutputStream outputStream = new FileOutputStream(f);
int readlen;
byte[] buf = new byte[1024];
while ((readlen = inputStream.read(buf)) > 0)
outputStream.write(buf, 0, readlen);
outputStream.close();
inputStream.close();
bitmap = BitmapFactory.decodeFile(filename);
return bitmap;
} catch (Exception e) {
return null;
}
For saving, make sure you have appropriate permissions if you're going to save to sdcard.
if you just want to load the image (without saving):
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Error getting bitmap", e);
}
return bm;
Just make sure you fetch the image in a separate thread or using AsyncTask or else you'll get ANRs!
you can try this ImageDownloader class from google. It´s works nice :)
Is an AsynkTask that handle the download and set the bitmap to an ImageView.
ImageDownloader
Usage:
private final ImageDownloader mDownload = new ImageDownloader();
mDownload.download("URL", imageView);

Android - Save image to SD and then Load Image

I've designed quickly a piece of code which loads from an specified URL and then saves it to the SD card, however it is not saving to the SD.
URL myFileUrl = new URL( Image_HTML);
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bm = BitmapFactory.decodeStream(is);
FileOutputStream fos = new FileOutputStream(filepath+image_name);
bm.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
bm = BitmapFactory.decodeFile(filepath+image_name);
image_loader_view.setImageBitmap(bm);
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i("Hub", "FileNotFoundException: "+ e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.i("Hub", "IOException: "+ e.toString());
}
I have tried to make this code as lightweight as possible, and I have also activated the EXTERNAL.STORAGE.WRITE in the android manifest.
Couple things.
When you use a FileOutputStream you have to make sure the directory you are trying to write to is created before you try to write a file to it. If not you have to create it. This can be done via mkdirs() method of the File class.
Next I'm not sure the call to getAbsolutePath is required, due to the type of file system Android uses. I've never had to use it to save to SD before.
I'd try these and see if one of them will solve it for you.

Categories

Resources