将图像从android上载到服务器php时出错?

将图像从android上载到服务器php时出错?,android,image-uploading,Android,Image Uploading,我想从android设备上传一个图像到php服务器,但我的应用程序出错。你能帮我修好吗?谢谢! 源代码 public class MainActivity extends Activity implements OnClickListener { private ImageView image; private Button btnselect; private Button btnupload; private Bitmap bmp; private HttpResponse respons

我想从android设备上传一个图像到php服务器,但我的应用程序出错。你能帮我修好吗?谢谢! 源代码

public class MainActivity extends Activity implements OnClickListener {

private ImageView image;
private Button btnselect;
private Button btnupload;
private Bitmap bmp;
private HttpResponse response;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    image = (ImageView) findViewById(R.id.image);
    btnselect = (Button) findViewById(R.id.btnselect);
    btnupload = (Button) findViewById(R.id.btnupload);
    btnselect.setOnClickListener(this);
    btnupload.setOnClickListener(this);
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
            R.drawable.electricinvoicesign);
    image.setImageBitmap(bitmapOrg);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btnselect:
        Intent choosePictureIntent = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(choosePictureIntent, 0);
        break;
    case R.id.btnupload:
        new upload().execute();
        break;
    default:
        break;
    }

}
public void hienthi()
{
    Toast.makeText(this, "Upload thanh cong!", Toast.LENGTH_LONG).show();
}

private class upload extends AsyncTask<Void, Void, Void>
{

    private ProgressDialog progressDialog;
    private BufferedReader in;


    @Override
    protected void onPostExecute(final Void unused) {
        try
        {
        this.progressDialog.dismiss();
        System.out.print("Upload thanh cong!");
        hienthi();
        in = new BufferedReader(new InputStreamReader(response
                .getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        String result = sb.toString();
        System.out.println(result);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPreExecute() {
        this.progressDialog = ProgressDialog.show(
                MainActivity.this, "", " Loading...");
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        try {
            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
                    R.drawable.electricinvoicesign);
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            bitmapOrg.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("image", ba1));
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                        "http://longvansolution.tk/uploadimage.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response=httpclient.execute(httppost);
            System.out.print("Upload thanh cong 2!");

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        return null;
    }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Uri imageFileUri = data.getData();
        try {
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            bmp = BitmapFactory
                    .decodeStream(
                            getContentResolver().openInputStream(
                                    imageFileUri), null, bmpFactoryOptions);
            bmpFactoryOptions.inJustDecodeBounds = false;
            bmp = BitmapFactory
                    .decodeStream(
                            getContentResolver().openInputStream(
                                    imageFileUri), null, bmpFactoryOptions);
            image.setImageBitmap(bmp);
        } catch (Exception e) {
            Log.v("ERROR", e.toString());
        }
    }
}
public类MainActivity扩展活动实现OnClickListener{
私有图像查看图像;
私人按钮b选择;
专用按钮btnupload;
私有位图bmp;
私有HttpResponse响应;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView)findviewbyd(R.id.image);
btnselect=(按钮)findViewById(R.id.btnselect);
btnupload=(按钮)findViewById(R.id.btnupload);
btselect.setOnClickListener(此);
btnupload.setOnClickListener(此);
位图bitmapOrg=BitmapFactory.decodeResource(getResources(),
R.可抽出式电气发票标志);
image.setImageBitmap(bitmapOrg);
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例R.id.B选择:
意向选择图片意向=新意向(
意向、行动、选择,
android.provider.MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);
startActivityForResult(选择PictureIntent,0);
打破
案例R.id.btnupload:
新建上载().execute();
打破
违约:
打破
}
}
公共空间hienthi()
{
Toast.makeText(这是“上传thanh-cong!”,Toast.LENGTH_LONG.show();
}
私有类上载扩展异步任务
{
私有进程对话;
中的私有缓冲区读取器;
@凌驾
后期执行时受保护的无效(最终无效未使用){
尝试
{
此.progressDialog.discouse()文件;
System.out.print(“上传thanh-cong!”);
hienthi();
in=新的BufferedReader(新的InputStreamReader(响应
.getEntity().getContent());
StringBuffer sb=新的StringBuffer(“”);
字符串行=”;
字符串NL=System.getProperty(“line.separator”);
而((line=in.readLine())!=null){
sb.追加(行+NL);
}
in.close();
字符串结果=sb.toString();
系统输出打印项次(结果);
}捕获(例外e)
{
e、 printStackTrace();
}
}
@凌驾
受保护的void onPreExecute(){
this.progressDialog=progressDialog.show(
MainActivity.this,“,”正在加载…);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TODO自动生成的方法存根
试一试{
位图bitmapOrg=BitmapFactory.decodeResource(getResources(),
R.可抽出式电气发票标志);
ByteArrayOutputStream bao=新建ByteArrayOutputStream();
压缩(Bitmap.CompressFormat.JPEG,90,bao);
字节[]ba=bao.toByteArray();
字符串ba1=Base64.encodeBytes(ba);
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“图像”,ba1));
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://longvansolution.tk/uploadimage.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
System.out.print(“上传thanh-cong 2!”);
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
返回null;
}
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
//TODO自动生成的方法存根
super.onActivityResult(请求代码、结果代码、数据);
if(resultCode==RESULT\u OK){
Uri imageFileUri=data.getData();
试一试{
Options bmpFactoryOptions=新的BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds=true;
bmp=位图工厂
.解码流(
getContentResolver().openInputStream(
imageFileUri),空,bmpFactoryOptions);
bmpFactoryOptions.inJustDecodeBounds=false;
bmp=位图工厂
.解码流(
getContentResolver().openInputStream(
imageFileUri),空,bmpFactoryOptions);
setImageBitmap(bmp);
}捕获(例外e){
Log.v(“错误”,例如toString());
}
}
}
}

这是一个错误:

11-15 13:59:04.642:W/System.err(17525):android.os.NetworkOnMainThreadException 11-15 13:59:04.642:W/System.err(17525):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 11-15 13:59:04.642:W/System.err(17525):位于libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163) 11-15 13:59:04.642:W/System.err(17525):位于libcore.io.IoBridge.recvfrom(IoBridge.java:503) 11-15 13:59:04.642:W/System.err(17525):位于java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) 11-15 13:59:04.642:W/System.err(17525):位于java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) 11-15 13:59:04.642:W/System.err(17525):位于java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) 11-15 13:59:04.642:W/System.err(17525):位于org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103) 11-15 13:59:04.642:W/System.err(17525):位于org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134) 11-15 13:59:04.642:W/System.err(17525):位于org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:161) 11-15 13:59:04.642:W/System.err(17525):位于org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159) 11-15 13:59:04.642:W/System.err(17525):位于java.io.InputStreamReader.rea