나는 안드로이드 응용 프로그램을해야 서버로 사진을 업로드하는 데 문제가 있습니다. 몇 가지 예제를 시도했지만 작동하지 않습니다.안드로이드에서 우분투 서버를 통해 사진 업로드
내보기는 갤러리 또는 카메라의 이미지를 선택하기위한 버튼으로 구성되어 있으며 다른 버튼을 사용하여 PHP 파일을 통해 서버에 업로드해야합니다.
class ImageGalleryTask extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... unsued) {
InputStream is;
BitmapFactory.Options bfo;
Bitmap bitmapOrg;
ByteArrayOutputStream bao ;
bfo = new BitmapFactory.Options();
bfo.inSampleSize = 2;
//bitmapOrg = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + customImage, bfo);
bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1 = Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("fotoUp",ba1));
nameValuePairs.add(new BasicNameValuePair("name","image_android"));
Log.v("log_tag", System.currentTimeMillis()+".jpg");
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
// Here you need to put your server file address
HttpPost("http://xxx.xxx.xxx.xxxx/xxxxxxxxxxxx/upload_photo.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.v("log_tag", "Success");
}catch(Exception e){
Log.v("log_tag", "Error in http connection "+e.toString());
}
return "Success";
// (null);
}
@Override
protected void onProgressUpdate(Void... unsued) {
}
@Override
protected void onPostExecute(String sResponse) {
try {
if (dialog.isShowing())
dialog.dismiss();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
와 PHP는 다음과 같습니다 :
$ruta = "photos/" . basename($_FILES['fotoUp']['name']);
if(move_uploaded_file($_FILES['fotoUp']['tmp_name'], $ruta))
chmod ("uploads/".basename($_FILES['fotoUp']['name']), 0644);
응용 프로그램이 작동을하지만 이미지가 서버에 업로드하지 않는
내 Android 코드는 다음과 같다. 우분투 서버에서 권한을 777로 변경합니다.
사진을 업로드 할 수있는 내 우분투 서버 폴더는 var/www/xxxx/photos에 있으며 PHP 파일은 var/www/xxx/upload_photo.php에 있습니다
또한 mySQL 데이터베이스에 저장되는 경로를 어떻게 저장할 수 있는지 알고 있습니다.
도움 주셔서 감사합니다.