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.
Related
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 was using the following function to convert a pdf to string:
private String GetString(String filepath) throws IOException {
InputStream inputStream = new FileInputStream(filepath);
String inputStreamToString = inputStream.toString();
byte[] byteArray = inputStreamToString.getBytes();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
return encoded;
}
I got the output as:
amF2YS5pby5GaWxlSW5wdXRTdHJlYW1ANTM1MDhmNTg=
I have found that this output is wrong. Because certainly when I encode a 2MB pdf file, it can't be so short. Actually I base64 decoded in php server and the output was an invalid pdf. So my question is what is missing in the function?
Ok. Problem solved. The following is the correct code to do this:
private String GetString(String filepath) throws IOException {
InputStream inputStream = new FileInputStream(filepath);
byte[] byteArray = IOUtils.toByteArray(inputStream);
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
return encoded;
}
Wrong part is: String inputStreamToString = inputStream.toString();. You'll not get the content of input stream as result, you'll get only something like "FileInputStream#021849".
You should read from stream in another way, e.g.: Android FileInputStream read() txt file to String
BTW, instead of reading file to String and converting it to byte array, you can read file straight to byte array.
You can solve it with the second code.
But "IOUtils" where the function code .
In My App,I have to decode the bytearray(that is in .h264 format) in to video and the bytearray coming from live steaming.The code is below,
static final int VIDEO_BUF_SIZE = 100000;
static final int FRAME_INFO_SIZE = 16;
byte[] frameInfo = new byte[FRAME_INFO_SIZE];
byte[] videoBuffer = new byte[VIDEO_BUF_SIZE];
File SDFile = android.os.Environment.getExternalStorageDirectory();
File destDir = new File(SDFile.getAbsolutePath() + "/test.h264");
//avRecvFrameData returns the length of the frame and stores it in ret.
int ret = av.avRecvFrameData(avIndex, videoBuffer,
VIDEO_BUF_SIZE, frameInfo, FRAME_INFO_SIZE,
frameNumber);
fos=new FileOutputStream(destDir,true);
fos.write(videoBuffer, 0, ret);
fos.close();
Here how can i decode the videobuffer(bytearray)?
So what Can I do now.
Thanks to All.
There is no official android api to decode a H.264 frame directly, the only way is porting ffmpeg to android and wrap the decode function with jni.
What is document missing error in google cloud print for android? I am searching for solutions but not yet found.. Kindly help me..
final Uri docUri = Uri.parse("/mnt/sdcard/downloads");
final String docMimeType = "pdf";
final String docTitle = "Android Interview Questions";
btn_print.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent printIntent = new Intent(PrintDialogActivity.this, PrintActivity.class);
printIntent.setDataAndType(docUri, docMimeType);
printIntent.putExtra("title", docTitle);
startActivity(printIntent);
}
});
As far as I know you have to convert the PDF in to base64 and then send it. The best approach to do this would be to use the built in Base64 class that comes with Android and use the method encodeToString to convert your file in to a base64 string.
How to convert file to bytes
File file = new File(path);
int size = (int) file.length();
byte[] bytes = new byte[size];
BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(file));
buffer.read(bytes, 0, bytes.length);
buffer.close();
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)
}