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)
}
Related
I have a question, how can I send my audio file to my server, I had tried to convert to base64 but nothing is working. This is my code
declaration of variables:
private MediaRecorder grabacion;
private String archivoSalida = null;
audio obtained with media record:
archivoSalida = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Myrecord.mp3";
grabacion = new MediaRecorder();
grabacion.setAudioSource(MediaRecorder.AudioSource.MIC);
grabacion.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
grabacion.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
grabacion.setOutputFile(archivoSalida);
Convert to base64,the error comes out here:
private String convertirAudString(MediaRecorder audio){
ByteArrayOutputStream array=new ByteArrayOutputStream();
byte[] audioByte = new byte[(int)audio.length()];//error in this line
String audioString = Base64.encodeToString(audioByte,Base64.DEFAULT);
return audioString;
}
Thanks for your all suggestion.
I solve this, with using
File file = new File(Environment.getExternalStorageDirectory() + "/Miaudio.mp3");
byte[] bytes = new byte[0];
bytes = FileUtils.readFileToByteArray(file);
Then I convert it to base64; it works for me very fast.
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.
I am trying to compress a video in Android before uploading. I am following a code from Stack, when I try it I can see that the file is compressed (not sure that it has actually compressed) but it reduces the file size and creates a compressed file as expected but I wont be able to open the file as its content is gibberish so I try to decompress the video as I can surely know that it has been successfully compressed which in turn has lead to decompression.
My problem is that the original file size and the decompressed file size is SAME, but the file does not open and it says "Sorry, the video cannot be played".
CODE :
Compression :
public static void compressData(byte[] data) throws Exception {
OutputStream out = new FileOutputStream(new
File("/storage/emulated/0/DCIM/Camera/compressed_video.mp4"));
Log.e("Original byte length: ", String.valueOf(data.length));
Deflater d = new Deflater();
DeflaterOutputStream dout = new DeflaterOutputStream(out, d);
dout.write(data);
dout.close();
Log.i("The Compressed Byte array is ", ""+data.length);
Log.e("Compressed byte length: ",
String.valueOf(dout.toString().getBytes().length));
}
Decompression :
public static void decompress() throws Exception {
InputStream in = new FileInputStream("/storage/emulated/0/DCIM/Camera/compressed_video.mp4");
InflaterInputStream ini = new InflaterInputStream(in);
ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
int b;
while ((b = ini.read()) != -1) {
bout.write(b);
}
ini.close();
bout.close();
String s = new String(bout.toByteArray());
System.out.println(s);
File decompressed_file = new File("/storage/emulated/0/DCIM/Camera/decompressed_video.mp4");
FileOutputStream out_file = new FileOutputStream(decompressed_file);
out_file.write(bout.toByteArray());
out_file.close();
Log.i("The Decompressed Byte array is ", ""+bout.toByteArray().length);
Log.e("De-compressed byte length: ",
String.valueOf(bout.toByteArray().length));
}
From the above code, the original byte length and the decompressed byte length is same but I am not sure why the byte array does not get write to the file. I can see that the two files of compressed_video and decompressed_video is created but I cant play either. Unable to play compressed_video.mp4 is acceptable but I should be able to play the decompressed_video.mp4 which is unavailable to play. I have been sitting on this for more than 2 days so any help would be insanely appreciated. Thanks in advance guys.
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.
I want to extract data returned by a third party web service. The response is parsed by the XmlPullParser. The block of data is the Base64 decoded TEXT of a single element. So far, my parser contains the code:
assert eventType == XmlPullParser.TEXT;
content = xpp.getText();
The content is the mentioned block of data. It works, but it can be 100+ kBytes long. I need to parse the content using another parser that would:
Decode the block of data encoded via base64. The result is the image of a zip file with a single zipped file inside.
Extract the content of the zipped file -- it is in CSV format.
Parse the lines of the CSV file and extract the data.
If I know the name of the file in the zip archive image, is it possible to process it on the fly using Android/Java objects? (On the fly -- I mean without storing it into the file first.) Or, how and where can I create a temporary file extracted from the zip file content?
Yes, you can parse those files on the fly.
byte[] decodedContent = Base64.decode(content, Base64.DEFAULT);
ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(decodedContent));
try{
ZipEntry entry = null;
while ((entry = zipStream.getNextEntry()) != null) {
String fileName = entry.getName();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
while ((count = zipStream.read(buffer)) != -1) {
baos.write(buffer, 0, count);
}
baos.close();
zipStream.closeEntry();
byte[] bytes = baos.toByteArray();
//Your own code to parse the CSV
parseCsvFile(fileName, bytes);
}
}finally{
zipStream.close();
}
Use this to decode from base64:
http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html
If you're developing for SDK 8 or higher you can also use:
http://developer.android.com/reference/android/util/Base64.html
Use this to unzip the decoded base64:
http://developer.android.com/reference/java/util/zip/ZipInputStream.html
Use a ByteArrayInputStrean to use the unzip with the decoded base64:
http://developer.android.com/reference/java/io/ByteArrayInputStream.html
And here's more to parsing a cvs file:
CSV API for Java