Facing Difficulty While Sending Mp3 File From Android To Server
I'am trying to send a mp3 file from my Android application to Server. I'am using this method android async http to send mp3 file. This is my code. try {
Solution 1:
Java Class
classUpLoadFilesextendsAsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */@OverrideprotectedvoidonPreExecute() {
super.onPreExecute();
pDialog = newProgressDialog(ProfileActivity.this);
pDialog.setMessage("Loading profile ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Profile JSON
* */protected String doInBackground(String... args) {
StringsourceFileUri="/mnt/sdcard/a.mp3";
System.out.println("file path:" + sourceFileUri);
HttpURLConnectionconn=null;
DataOutputStreamdos=null;
StringlineEnd="\r\n";
StringtwoHyphens="--";
Stringboundary="*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
intmaxBufferSize=1 * 1024 * 1024;
FilesourceFile=newFile(sourceFileUri);
////System.out.println("file exists:" + sourceFile + " Email " + email);try {
StringupLoadServerUri="http:/pairdroid.com/fileupload.php?";
String filenameArray[] = sourceFile.getName().split("\\.");
Stringextension= filenameArray[filenameArray.length - 1];
// open a URL connection to the ServletFileInputStreamfileInputStream=newFileInputStream(
sourceFile);
URLurl=newURL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("bill", sourceFileUri);
dos = newDataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
+ sourceFileUri + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = newbyte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file// data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
StringserverResponseMessage= conn.getResponseMessage();
if (serverResponseCode == 200) {
////System.out.println("Uploaded http: 200//");// messageText.setText(msg);// Toast.makeText(ctx, "File Upload Complete.",// Toast.LENGTH_SHORT).show();// recursiveDelete(mDirectory1);
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (Exception e) {
// dialog.dismiss();
e.printStackTrace();
}
// dialog.dismiss();return"Executed";
}
/**
* After completing background task Dismiss the progress dialog
* **/protectedvoidonPostExecute(String file_url) {
}
}
php code(fileupload.php)
<?php$uploads_dir = './';
$tmp_name = $_FILES['bill']['tmp_name'];
$pic_name = $_FILES['bill']['name'];
if (move_uploaded_file($tmp_name, $uploads_dir.$pic_name )) {
}
?>
Post a Comment for "Facing Difficulty While Sending Mp3 File From Android To Server"