I am attempting to upload an Image to my Server using several sources that I have discovered Online. I have been attempting to do this in several Ways, however none of them appear to work. Essentially, I have an Image that I woud like to Upload to the server From my Device. The connection is successfull, and the PHP file on the server appears to be responding. Here is my Code to do the Uploading. Note that this is Wrapped inside an AsyncTask:
ANDROID:
File imgFile = new File("/storage/emulated/0/Pictures/Screenshots/Screenshot_2015-02-22-20-52-02.png");
String encoded = ""; // ENCODED IMAGE
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image", encoded));
nameValuePairs.add(new BasicNameValuePair("filename", "testImage.jpeg"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ift.tt/1LwsxnM");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
BufferedReader in = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
String responseString = sb.toString();
Log.e("RESPUPOAD", "RSP: " + responseString);
} catch (Exception e) {
}
}
AND THE PHP ON THE SERVER:
<?php
// Get image string posted from Android App
$base=$_REQUEST['image'];
// Get file name posted from Android App
$filename = $_REQUEST['filename'];
// Decode Image
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
// Images will be saved under 'www/imgupload/uplodedimages' folder
$file = fopen('images/'.$filename, 'wb');
// Create File
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete, Please check your php file directory';
?>
I have set the Permission for the Folders on the Server to: 777 in an attempt to fix the issue. But that didn't fix anything. I have also tried storing the Image directly in the Database as a BLOB, but that resulted in much Longer Loading times for the Data. Could it be that the PHP path is incorrect? Do I have to set the Path differently?
What am I doing wrong here? And what are the Alternatives to this? An answer would be beneficial to a lot of People since I have found that a lot of People are experiencing similar Issues.
Aucun commentaire:
Enregistrer un commentaire