Good day to all. I am having clarification about to send multiple files to server. I have completed that send single file to server. Below code is detail about send single file to server.
private static final int FILE_SELECT_CODE = 0;
public String TAG = "MainActivity";
Uri uri;
String path = null, filename;
File filede;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
fileName = (TextView) findViewById(R.id.fileName);
filePath = (TextView) findViewById(R.id.filePath);
fileCount = (TextView) findViewById(R.id.fileCount);
btn = (Button) findViewById(R.id.btn);
upload = (Button) findViewById(R.id.btnup);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileChooser();
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadFile(path, filename);
}
});
}
Once we called show file chooser method i have to intent to add category and with in the activity result i have to file name, file and file path. that activity result is
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
Log.e(TAG, "File Uri: " + uri.toString());
// Get the path
path = FilePath.getPath(MainActivity.this, uri);
filede = new File(path);
filename = filede.getName();
Log.e(TAG, "File Path: " + path);
Log.e(TAG, "File: " + filede);
Log.e(TAG, "File filename: " + filename);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
With the help of file path and file name i have uploaded file to server.
private void uploadFile(String filePath, String fileName) {
Log.e(TAG, "File filename:----------->uploadFile "+filePath+", "+fileName);
InputStream inputStream;
try {
inputStream = new FileInputStream(new File(filePath));
Log.e(TAG, "File inputStream:-----------> "+inputStream);
byte[] data;
try {
data = IOUtils.toByteArray(inputStream);
Log.e(TAG, "File data:-----------> "+data);
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("Your URL");
Log.e(TAG, "File httpPost:-----------> "+httpPost);
InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), fileName);
Log.e(TAG, "File inputStreamBody:-----------> "+inputStreamBody);
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("fileContent0", inputStreamBody);
multipartEntity.addPart("id", new StringBody("331"));
multipartEntity.addPart("fileCount", new StringBody("1"));
multipartEntity.addPart("fileType", new StringBody("SAMPLE"));
multipartEntity.addPart("platform", new StringBody("Android"));
multipartEntity.addPart("externalID", new StringBody(""));
httpPost.addHeader("Authorization","");
Log.e(TAG, "File multipartEntity:-----------> "+multipartEntity);
httpPost.setEntity(multipartEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.e(TAG, "File httpResponse:----------->httpResponse "+httpResponse.getStatusLine());
BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
JSONObject o = new JSONObject(result.toString());
Log.e("oooooo","ooooooo------------------>"+o);
// Handle response back from script.
if (httpResponse != null) {
Log.e(TAG, "File httpResponse111:----------->httpResponse11 "+httpResponse);
} else { // Error, no response.
}
} catch (IOException e) {
Log.e("error","error--------->"+e);
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
Here i am passing single file name and single file path for uploading single file. Actually my requirement is i have to pass multiple file name and multiple file path. And How to store file path and file name into an array list when each file has selected separately? And how to get the multiple file response from server. Thanks in advance friends. Kindly please review it and help me.