Creating File With String Object - android

im writing a socket program which send and receive strings.
Im trying to build a file using this received strings that are sending from other side.
this is exactly done with text files but in others such an image file doesn't work and when complete, the image does not open!
in receiver:
file = new File(dir,FileName);
fOut = new FileOutputStream(file);
dos = new DataOutputStream(fOut);
and when a msg received:
dos.write(msg.getBytes("UTF-8"));
in sender:
File file = new File(FilePath);
InputStream is = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(is);
...
isr.read(inputBuffer);
I have Tried this solution but the problem didn't fixed:
Changing the UTF-8 to ISO-8859-1
using output stream writer instead of data output stream.
trying with smaller pictures than text file.

ByteArrayOutputStream is useful converting bitmap to byte array.
after then, you using BitmapFactory, decodeByteArray is converting byte to bitmap.

Related

Audio file to byte array Android not getting converted correctly

I am trying to convert audio file to the byte array, but it seems it is not getting converted correctly. I am recording sound using mic, then converting that file to byte array using file's path on the device.
The desired byte array should be like 0x12323
But it is coming like this string [B#14746f6
Below is the code to convert audio to byte array
file is the path of the file on the device. File type is amr
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int read = 0;
byte[] buffer = new byte[1024];
while (read != -1) {
read = fis.read(buffer);
if (read != -1)
out.write(buffer,0,read);
}
out.close();
byte[] bytes = out.toByteArray();
Log.e("byte array" ,bytes.toString());
String path= ""; // Audio File path
InputStream is= new FileInputStream(path);
byte[] arr= readByte(is);
Log.e("byte: ",""+ Arrays.toString(arr));
I solved this issue after talking to api guy. I converted byte array to base64 string and passed it. Which resolved the issue.

Encoding video file in android and decode in php

in my android application i encode a video as base 64 like this.
File file=new File(path);
InputStream is = new FileInputStream(file);
int length = (int)file.length();
byte[] bytes = new byte[length];
int a=is.read(bytes,0,length);
String str = Base64.encodeToString(bytes, 0);
is.close();
//send the string to my server....
PHP
$str=$_POST['str'];
$var=base64_decode($str);
$fp = fopen('2013-02-21_14-52-35_968.mp4', 'w');
fwrite($fp,$var);
fclose($fp);
So when the video file is Written, i cant open it. How i can correctly encode a video and decode it from PHP? or what im missing thanks in advanced.
I solve my problem, the issue was I only encode one part of the file. Here my solution:
$fp=fopen("/address".$filename,'w')
while($row=mysql_fetch_array($getChunks)){
$chuncks=$row['chunkpart'];
$var=base64_decode($chunks);
fwrite($fp,$var)
}

Saving a string serialized and buffered android JAVA.IO ObjectInputStream

I am saving out and loading mixed data types. I either have the saving part wrong or the loading part wrong. I am using buffered serial save and load method.
Variable lastFetchDate is defined as a string and initialized as "00/00/00".
It throws an error when reloading the data after it has been saved. What is wrong? I would have thought the opposite to writeBytes would be readBytes for a string.
Saving is as follows:
FileOutputStream fos = new FileOutputStream("userPrefs.dat");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeBytes(lastFetchDate);
// I close all streams
Loading is as follows:
FileInputStream fis = new FileInputStream("userPrefs.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);
lastFetchDate=(String)ois.readObject(); //<<<<< Error thrown here
// I close all streams
You have written string as byte[] so need to read as byte[]
byte [] bString = new byte[lastFetchDate.length()*2];
ois.readFully(bString, 0, bString.length);
Or if you write as Object using writeObject method then you can read as object,
oos.writeObject(lastFetchDate);

Trying to figure out how to write an array out to a file stream in Android

In my app on the simulator it takes like 20 secs to save a file 48k long. Right now I'm saving byte by byte. Using a file stream, FileOutputStream write function.
Which looks like fos.write(cGlobals.board.BitMap[c++]);
I tried to do this but got a compile error saying invalid parm
fos.write(cGlobals.board.BitMap);
Is there a better way of doing this then byte by byte?
Ted
Make a BufferredOutputStream around your FileOutputStream
FileOutputStream fileOutputStream = new FileOutputStream(.....);
OutputStream bos = new BufferedOutputStream(fileOutputStream, 8192);
try {
... do your stuff using bos instead of fileOutputStream
} finally {
bos.close();
}

Sending File using sockets in android

I know basic socket programming.
I have a code to send strings using sockets in android.
I want to learn how to send a file (MP3,image etc) using sockets between two phones.
This is some code to send a file. It should work just like you would expect outside of Android. I knew I was sending files that were relatively small, so you might want to make more than one pass through a buffer. The File "f" in my example should just be replaced with the File that contains your MP3 or Image or whatever you want to send.
public void sendFile() throws IOException{
socket = new Socket(InetAddress.getByName(host), port);
outputStream = socket.getOutputStream();
File f = new File(path);
byte [] buffer = new byte[(int)f.length()];
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(buffer,0,buffer.length);
outputStream.write(buffer,0,buffer.length);
outputStream.flush();
}

Categories

Resources