i need to select a picture from gallery and post it through a rest service. i am able to select an image and display it in imageView when i click post i get a Verbose error showing FileNotFoundException
try{
url = new URL("http://webservices.EducateMax.com/EducateMax.svc/PostInfractionsInfo?sPhoto="+sPicturePath+"&sTitle=sample");
}catch (MalformedURLException e){
Toast.makeText(getApplicationContext(),"Check the URL",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
final List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("sPhoto", sPicturePath));
nameValuePairs.add(new BasicNameValuePair("sTitle", "sample"));
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
MultiPartServer1.postData(url, nameValuePairs);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
MultiPartServer.java
private static final String TAG = "MultipartServer";
private static String crlf = "\r\n";
private static String twoHyphens = "--";
private static String boundary = "*****";
private static String avatarPath = null;
public static String postData(URL url, List<NameValuePair> nameValuePairs) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
String avatarName = null;
StringBuilder query = new StringBuilder();
boolean first = true;
for (NameValuePair pair : nameValuePairs) {
if (first)
first = false;
else
query.append("&");
query.append(URLEncoder.encode(pair.getName(), "UTF-8"));
query.append("=");
query.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
if ((avatarName = pair.getName()).equals("avatar")) {
avatarPath = pair.getValue();
}
}
FileInputStream inputStream;
OutputStream outputStream = connection.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeBytes(query.toString());
if(avatarName != null && avatarPath != null) {
dataOutputStream.writeBytes(twoHyphens + boundary + crlf);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"" + avatarName + "\";filename=\"" + new File(avatarPath).getName() + "\";" + crlf);
dataOutputStream.writeBytes(crlf);
inputStream = new FileInputStream(avatarPath);
byte[] data = new byte[1024];
int read;
while((read = inputStream.read(data)) != -1)
dataOutputStream.write(data, 0, read);
inputStream.close();
dataOutputStream.writeBytes(crlf);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
}
dataOutputStream.flush();
dataOutputStream.close();
String responseMessage = connection.getResponseMessage();
Log.d(TAG, responseMessage);
InputStream in = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder response = new StringBuilder();
char []b = new char[512];
int read;
while((read = bufferedReader.read(b))!=-1) {
response.append(b, 0, read);
}
connection.disconnect();
Log.d(TAG, response.toString());
return response.toString();
}
What I have tried:
selecting an image from gallery, display it in an ImageView. MultipartServer class is intended to HTTP operations