Reputation: 2504
Based on the recommendations of a previous post I'm trying to use Android: Uploading image on server with php however I get a file not found exception.
Here's my function as described in the post above. My input for these are:
Gallery: uploadFile: Source File not exist :content://media/external/images/media/342
Photo: uploadFile: Source File not exist: file:///storage/emulated/0/MyDir/blah
These uri's are derived from the intent a lanched to catputre/select them. Any ideas why I get a File Not Found
exception?
private void doFileUpload(String exsistingFileName){
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
//String exsistingFileName = "/sdcard/six.3gp";
// Is this the place are you doing something wrong.
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String urlString = "http://192.168.1.5/upload.php";
try
{
Log.e("MediaPlayer","Inside second Method");
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e("MediaPlayer","Headers are written");
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
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);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
String LogString = "";
while ((inputLine = in.readLine()) != null) {
LogString= LogString + inputLine;
}
Log.i(Utils.TAG, LogString);
// close streams
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
}
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream ( conn.getInputStream() );
String str;
while (( str = inStream.readLine()) != null)
{
Log.e("MediaPlayer","Server Response"+str);
}
/*while((str = inStream.readLine()) !=null ){
}*/
inStream.close();
}
catch (IOException ioex){
Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
}
}
Upvotes: 3
Views: 6165
Reputation: 5271
In such scenarios, there can be different reasons. First, make sure that you have added READ_EXTERNAL_STORAGE permission in your AndroidManifest, and also you have added Runtime permissions for API level 19 and above. Then one of the reasons could be mentioned in the accepted answer. But those who are looking for a full detailed solution to upload all types of files to Server in Android can follow this article. Upload File to Server in Android. It describes all steps from picking file to the uploading.
Upvotes: 0
Reputation: 1007584
i get a file not found exception
That is because neither of those are paths to files. You can tell that by looking at them. You also did not follow the instructions from my previous answer.
Replace:
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
with:
InputStream contentInputStream = getContentResolver().openInputStream(Uri.parse(exsistingFileName));
(and replace occurrences of fileInputStream
with contentInputStream
for the rest of your method)
Note that:
This assumes that your doFileUpload()
is implemented on some class that inherits from Context
, such as an Activity
or a Service
. You will need to arrange to get a ContentResolver
to doFileUpload()
by other means if doFileUpload()
does not have access to getContentResolver()
.
You could simplify matters a bit by passing in the Uri
you received into doFileUpload()
, rather than converting it to a String
and then back into a Uri
.
You will need to invent your own filename for the Content-Disposition:
header, as you do not get a filename from the Uri
.
Upvotes: 3
Reputation: 8621
You are trying to get an InputStream from a Uri ? This might be easier :
private void doFileUpload(Uri fileUri){
// some code
InputStream inputStream = getContentResolver().openInputStream(fileUri);
// more code
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + "some_file_name" +"\"" + lineEnd);
// the rest of the code
}
Upvotes: 0