【发布时间】:2014-11-07 11:01:39
【问题描述】:
我正在将图像上传到按钮单击片段中的服务器。用户可以点击上传按钮,选择一张图片,然后将其发布到服务器。问题是用户从手机中选择图片,当点击发布按钮时,它没有上传到服务器。当我将此代码与 Activity 一起使用时,此代码可以完美运行。但是当我将它与片段一起使用时,它不起作用。请给出解决方案 我的代码是
image.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
if (requestCode == 1)
{
// currImageURI is the global variable I’m using to hold the content:
currImageURI = data.getData();
}
}
}
//Convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri)
{
String [] proj={MediaStore.Images.Media.DATA};
android.database.Cursor cursor = getActivity().getContentResolver().query( contentUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
//AsyncTask--------------------------------------------------------------------------------------------------------
//---------------------------using AsyncTask here------------------------------------------------------------------
public class HttpUploader extends AsyncTask<String, Void, String>
{
protected String doInBackground(String...path)
{
for (String sdPath : path)
{
Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
//Resize the image
double width = bitmapOrg.getWidth();
double height = bitmapOrg.getHeight();
double ratio = 400/width;
int newheight = (int)(ratio*height);
System.out.println("———-width" + width);
System.out.println("———-height" + height);
bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true);
//Here you can define .PNG as well
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao);
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeToString(ba,Base64.DEFAULT);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image", ba1));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url+"/api.upload.php?q="+URLEncoder.encode(Mobile,"UTF-8"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// print responce
outPut = EntityUtils.toString(entity);
Log.i("GET RESPONSE—-", outPut);
//is = entity.getContent();
Log.e("log_tag ******", "good connection");
bitmapOrg.recycle();
} catch (Exception e) {
Log.e("log_tag ******", "Error in http connection " + e.toString());
}
}
return outPut;
}
}
【问题讨论】:
-
“它不工作” ==> LogCat、stacktrace、预期/实际行为?
标签: android android-fragments actionbarsherlock server