나는 코드 스 니펫을 결합하여 효과가있는 것을 얻었습니다.
public class uploadImage extends AsyncTask<Object, Void, HttpEntity>{
@Override
protected HttpEntity doInBackground(Object... params){
DefaultHttpClient client = new DefaultHttpClient();
String url= IMAGE_URL+"?auth_token=" + auth_token;
Log.d(TAG, "image_url: " + url);
HttpPost post = new HttpPost(url);
MultipartEntity imageMPentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try{
imageMPentity.addPart("project_id", new StringBody(projectID));
imageMPentity.addPart("step_id", new StringBody(stepID));
imageMPentity.addPart("content_type", new StringBody("image/jpeg"));
imageMPentity.addPart("filename", new StringBody(filename));
imageMPentity.addPart("imagePath", new FileBody(new File(filepath)));
post.setEntity(imageMPentity);
} catch(Exception e){
Log.e(StepActivity.class.getName(), e.getLocalizedMessage(), e);
}
HttpResponse response = null;
try {
response = client.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity result = response.getEntity();
return result;
}
protected void onPostExecute(HttpEntity result){
if(result !=null){
// add whatever you want it to do next here
}
}
}
asynctask에 파일 경로와 파일 이름이 필요합니다. 내 응용 프로그램에서는 사용자가 갤러리에서 이미지를 선택할 수있었습니다. 그런 다음 파일 경로와 파일 이름을 검색합니다.
@Override
// user selects image from gallery
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){
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);
Log.d(TAG, "picturePath: " + picturePath);
filepath = picturePath;
filename = Uri.parse(cursor.getString(columnIndex)).getLastPathSegment().toString();
Log.d(TAG, "filename: " + filename);
cursor.close();
// add the image to the view
addedImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
희망이 있습니다.
현재이 문제가 있습니다. 문제를 해결할 수 있었습니까? – bodacious
나는 아래 답변으로 일을 끝낸 것을 올렸다. 궁금한 점이 있으면 알려주세요. – scientiffic