6
在我的應用程序中,我將圖像從我的設備上傳到本地網絡服務器... 執行代碼後,服務器中創建了一個.jpg文件,但它並未打開。 而服務器中文件的大小與原始文件不同。將圖像從android上傳到PHP服務器
Android的活動: -
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSelectImage=(Button) findViewById(R.id.uploadButton);
btnSelectImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data!=null) {
Uri selectedImage=data.getData();
String[] filePathColumn={MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap=BitmapFactory.decodeFile(picturePath);
ImageView im = (ImageView) findViewById(R.id.imgBox);
im.setImageBitmap(bitmap);
/*
* Convert the image to a string
* */
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr,Base64.DEFAULT);
/*
* Create a name value pair for the image string to be passed to the server
* */
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
JSONObject jsonString=new JSONObject();
try {
jsonString.put("img", image_str);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new uploadImageToPhp().execute(jsonString);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class uploadImageToPhp extends AsyncTask<JSONObject, Void, Void>
{
String dataToSend=null;
public static final String prefix="http://"; //prefix of the urls
public static final String server_ip="172.16.26.155"; //the ip address where the php server is located
public static final String completeServerAddress=prefix+server_ip+"/test_upload/upload_image.php"; //Exact location of the php files
@Override
protected Void doInBackground(JSONObject... params) {
dataToSend="image="+params[0];
communicator(completeServerAddress, dataToSend);
return null;
}
public void communicator(String urlString,String dataToSend2)
{
String result=null;
try
{
URL url=new URL(urlString);
URLConnection conn=url.openConnection();
HttpURLConnection httpConn=(HttpURLConnection) conn;
httpConn.setRequestProperty("Accept", "application/json");
httpConn.setRequestProperty("accept-charset", "UTF-8");
httpConn.setRequestMethod("POST");
httpConn.connect();
//Create an output stream to send data to the server
OutputStreamWriter out=new OutputStreamWriter(httpConn.getOutputStream());
out.write(dataToSend2);
out.flush();
int httpStatus = httpConn.getResponseCode();
System.out.println("Http status :"+httpStatus);
if(httpStatus==HttpURLConnection.HTTP_OK)
{
Log.d("HTTP STatus", "http connection successful");
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
StringBuilder sb = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
sb.append(inputLine+"\n");
}
in.close();
result=sb.toString();
try
{
//jsonResult = new JSONObject(result);
}
catch(Exception e)
{
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
}
else
{
System.out.println("Somthing went wrong");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
PHP代碼: -
$recievedJson=$_REQUEST['image'];
$imageContent=json_decode($recievedJson,true);
$base=$imageContent["img"];
$binary=base64_decode($base);
echo $binary;
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
我不能使用HttpURLConnection的??? –
我對所有其他通信都使用httpurlconnection,所以我在考慮使用相同的方法上傳圖片, –
但這是上傳圖片最簡單的方法,此代碼完全適合我。 – InnocentKiller