Save file object to internal storage as image - android

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();
}

Related

FTPClient in android convert image to bitmap

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();
}

Is there any way to copy byte by byte from a file and play it using videoView in android?

I have tried this but it's not working. It copies all the bytes at once and play it. But i want to copy byte by byte from a file and play this video during/while receiving...
Some Code
tempFile = new File(Environment.getExternalStorageDirectory()+ "/" +"temp.mp4");
int length=0;
byte[] vidBytes = new byte[(int) file.length()];
try {
//while (length<100)
//{
fileInputStream = new FileInputStream(file);
fileInputStream.read(vidBytes,0,vidBytes.length);
fileInputStream.close();
videoPlayer.setVideoPath(tempFile.getAbsolutePath());
Toast.makeText(this,tempFile.getPath(),Toast.LENGTH_LONG).show();
videoPlayer.start();
length++;
//}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
fileOutputStream.write(vidBytes,0,50,vidBytes.length);
fileOutputStream.close();
what the hell are you doing? the fileInputStream is practically useless ! you read the bytes into the byte array vidBytes, but never actually use it anywhere, videoView doesn't require you to open an input stream, all videoView requires is the path to the file and the videoView code takes care of opening the stream and loading the data efficiently.
Also why are you writing the same Byte array that you read just few statements ago not modify it and write it back to the SAME FILE!? What are you trying to accomplish here?
P.S This is all you need to do to play the mp4 file in the videoview
tempFile = new File(Environment.getExternalStorageDirectory()+ "/" +"temp.mp4");
videoPlayer.setVideoPath(tempFile.getAbsolutePath());
Toast.makeText(this,tempFile.getPath(),Toast.LENGTH_LONG).show();
videoPlayer.start();

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....

Android FTP download exception (file not found)

I am trying to retrieve a file via FTP but I am getting the following error in LogCat:
java.io.FileNotFoundException :/config.txt (read-only file system)
I have verified that the file exists on the server, and I can read it by double clicking it in a web browser.
Can anyone help please? Here is the code I am using:
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect("xxx.xxx.xxx.xxx");
client.enterLocalPassiveMode();
client.login("user", "pass");
//
// The remote file to be downloaded.
//
String filename = "config.txt";
fos = new FileOutputStream(filename);
//
// Download file from FTP server
//
client.retrieveFile("/" + filename, fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
You cannot save files on the root of the phone. Use Environment.getExternalStorageDirectory()to get an file object of the SD card's directory and save the file there. Maybe create a directory for it. To do that you need the permission android.permission.WRITE_EXTERNAL_STORAGE.
Sample code:
try {
File external = Environment.getExternalStorageDirectory();
String pathTofile = external.getAbsolutePath() + "/config.txt";
FileOutputStream file = new FileOutputStream(pathTofile);
} catch (Exception e) {
e.printStackTrace();
}
You are trying to read the file in, but you have created an OutputStream, you need to create an inputStream and then read the file from that input stream.
Here is a great article, with come code that is very helpful. This should get you headed in the right direction.
http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml
I hope this helps!
Best of luck

How to read file from phone's internal memory in android?

I have downloaded a file from HttpConnection using the FileOutputStream in android and now its being written in phone's internal memory on path as i found it in File Explorer
/data/data/com.example.packagename/files/123.ics
Now, I want to open & read the file content from phone's internal memory to UI. I tried to do it by using the FileInputStream, I have given just filename with extension to open it but I am not sure how to mention the file path for file in internal memory,as it forces the application to close.
Any suggestions?
This is what I am doing:
try
{
FileInputStream fileIn;
fileIn = openFileInput("123.ics");
InputStream in = null;
EditText Userid = (EditText) findViewById(R.id.user_id);
byte[] buffer = new byte[1024];
int len = 0;
while ( (len = in.read(buffer)) > 0 )
{
Userid.setText(fileIn.read(buffer, 0, len));
}
fileIn.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
String filePath = context.getFilesDir().getAbsolutePath();//returns current directory.
File file = new File(filePath, fileName);
Similar post here
read file from phone memory
If the file is where you say it is, and your application is com.example.packagename, then calling openFileInput("123.ics"); will return you a FileInputStream on the file in question.
Or, call getFilesDir() to get a File object pointing to /data/data/com.example.packagename/files, and work from there.
I am using this code to open file in internal storage. i think i could help.
File str = new File("/data/data/com.xlabz.FlagTest/files/","hello_file.xml");

Categories

Resources