Note that there are some explanatory texts on larger screens.

plurals
  1. POJava file uploading using PHP : Not working
    primarykey
    data
    text
    <p>I'm trying to upload an image file on button click. I used the same php script for iOS and it seems to work fine. I have a added the code used to upload the file. What I suspect is that I'm not passing in the image file path correctly. I have infact added the saving image file method. I just save it as 'sign.jpeg'. I don't see the image on button click pointed to uploadfile(View view) method.</p> <pre><code> public void uploadFile(View view) { try { upload("sign.jpeg"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } </code></pre> <p><strong>method implementing the file upload</strong> </p> <pre><code>public void upload(String selectedPath) throws IOException { HttpURLConnection connection = null; DataOutputStream outputStream = null; DataInputStream inputStream = null; String pathToOurFile = selectedPath; String urlServer = "http://localhost:8888/uploadJava.php"; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; try { FileInputStream fileInputStream = new FileInputStream(new File( pathToOurFile)); URL url = new URL(urlServer); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream .writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";name=\"" + "sign.jpeg" + "\"" + lineEnd); outputStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead &gt; 0) { outputStream.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); /*int serverResponseCode = connection.getResponseCode(); String serverResponseMessage = connection.getResponseMessage();*/ BufferedReader in = new BufferedReader( new InputStreamReader( connection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); fileInputStream.close(); outputStream.flush(); outputStream.close(); } catch (Exception ex) { } } </code></pre> <p><strong>php script to upload the file</strong></p> <pre><code>&lt;?php $target_path = "./images/"; $target_path = $target_path . basename($_FILES['uploadedfile']['name']); error_log("Upload File &gt;&gt;" . $target_path . $_FILES['error'] . " \r\n", 3, "Log.log"); error_log("Upload File &gt;&gt;" . basename($_FILES['uploadedfile']['name']) . " \r\n", 3, "Log.log"); if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file " . basename($_FILES['uploadedfile']['name']) . " has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } ?&gt; </code></pre> <p><strong>Saving picture taken from the camera after cropping</strong></p> <pre><code>PictureCallback mPicture = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { InputStream is = new ByteArrayInputStream(data); Bitmap bmp = BitmapFactory.decodeStream(is); bmp = Bitmap.createBitmap(bmp, 50, 200, bmp.getWidth() - 200, bmp.getHeight()/2); saveImage("sign.jpeg", bmp, getApplicationContext()); try { FileOutputStream fos; fos= openFileOutput(fileName,Context.MODE_PRIVATE); System.out.println(data); fos.write(data); fos.close(); } catch (Exception e) { e.printStackTrace(); }*/ finish(); //releaseCamera(); } }; </code></pre> <p><strong>image saving method</strong></p> <pre><code> private void saveImage(String filename, Bitmap b, Context ctx){ try { ObjectOutputStream oos; FileOutputStream out;// = new FileOutputStream(filename); out = ctx.openFileOutput(filename, Context.MODE_PRIVATE); oos = new ObjectOutputStream(out); b.compress(Bitmap.CompressFormat.PNG, 100, oos); oos.close(); oos.notifyAll(); out.notifyAll(); out.close(); } catch (Exception e) { e.printStackTrace(); } } </code></pre> <p>And yes the Internet permission is added in the AndroidManifest.xml as below</p> <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"/&gt; </code></pre> <p>Any idea what is wrong with the code above?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload