Vote count:
0
I am trying to upload a image to server with web service for that primarily I am picking image from gallery and converting the image into byte array and trying to post the byte array value unfortunately its not doing well here is my complete
public class MainActivity extends Activity {
private ImageView ivGalImg;
private Button uploadButton;
private Button selectImageButton;
static String picturePath;
private TextView tv;
Bitmap bmp;
String image;
byte[] imageInByte;
String filePath;
// number of images to select
private static int RESULT_LOAD_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivGalImg = (ImageView) findViewById(R.id.uploadImage);
uploadButton = (Button) findViewById(R.id.uploadButton);
tv = (TextView)findViewById(R.id.textView1);
// on click select an image
selectImageButton = (Button) findViewById(R.id.selectImageButton);
selectImageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectImageFromGallery();
}
});
// when uploadButton is clicked
uploadButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// new ImageUploadTask().execute();
uploadPhoto();
}
});
}
protected void selectImageFromGallery() {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
protected void uploadPhoto() {
if(bmp != null && !bmp.isRecycled())
{
bmp = null;
}
bmp = BitmapFactory.decodeFile(filePath);
ivGalImg.setBackgroundResource(0);
ivGalImg.setImageBitmap(bmp);
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream1);
byte[] imageInByte = stream1.toByteArray();
//image = imageInByte.toString();
image = Base64.encodeToString(imageInByte,Base64.DEFAULT);
System.out.println("The Byte Array Value is......."+image);
tv.setText(filePath);
new UploadPhoto().execute();
}
private class UploadPhoto extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... param) {
// TODO Auto-generated method stub
String X1 = "100";
String Y1 = "100";
String X2 = "100";
String Y2 = "100";
// int Y2 = 100;
// String UserID = "197";
int UserId = 197;
String FileName = "1427468399935.jpeg";
String PhotoArray = image;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("PhotoArray", PhotoArray));
params.add(new BasicNameValuePair("FileName", FileName));
params.add(new BasicNameValuePair("UserID", Integer.toString(UserId)));
params.add(new BasicNameValuePair("X1", X1));
params.add(new BasicNameValuePair("Y1", Y1));
params.add(new BasicNameValuePair("Y2", Y2));
params.add(new BasicNameValuePair("X2", Y2));
ServiceHandler shpd = new ServiceHandler();
String post_a_deal = getString(R.string.post_a_deal);
String postDb = shpd.makeServiceCall(post_a_deal, ServiceHandler.POST,params);
Log.d("Create Prediction Request: ", "> " + postDb);
return null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultcode, Intent intent) {
super.onActivityResult(requestCode, resultcode, intent);
if (requestCode == 1)
{
if (intent != null && resultcode == RESULT_OK)
{
Uri selectedImage = intent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
}
else
{
Log.d("Status:", "Photopicker canceled");
}
}
}
}
Here Is my Servicehandler Class code
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
Thanks in advance
asked 56 secs ago
Android, Unable to post append byte array to web service
Aucun commentaire:
Enregistrer un commentaire