0

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());
        }
    }
}

}

これはエラーです:

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.read( InputStreamReader.java:244) 11-15 13:59:04.642: W/System.err(17525): java.io.BufferedReader.fillBuf(BufferedReader.java:130) 11-15 13:59:04.642: W/ System.err(17525): java.io.BufferedReader.readLine(BufferedReader.java:390) 11-15 13:59:04.642: W/System.err(17525): com.example.uploadimage.MainActivity$upload で.onPostExecute(MainActivity.java:99) 11-15 13:59:04.642: W/System.err(17525): com.example.uploadimage.MainActivity$upload.onPostExecute(MainActivity.java:1) 11-15 13: 59:04.642: W/System.err(17525): android.os.AsyncTask.finish(AsyncTask.java:602) 11-15 13:59:04.642: W/System.err(17525): android.os で.AsyncTask.access$600(AsyncTask.java:156) 11-15 13:59:04.642: W/System.err(17525): android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) で 11-15 13:59:04.642: W/System.err(17525): android.os.Handler.dispatchMessage(Handler.java:99) 11-15 13:59:04.642: W/System.err(17525): Android で.os.Looper.loop(Looper.java:137) 11-15 13:59:04.642: W/System.err(17525): android.app.ActivityThread.main(ActivityThread.java:4514) で 11-15 13 :59:04.642: W/System.err(17525): java.lang.reflect.Method で。invokeNative(ネイティブ メソッド) 11-15 13:59:04.650: W/System.err(17525): java.lang.reflect.Method.invoke(Method.java:511) 11-15 13:59:04.650: W /System.err(17525): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 11-15 13:59:04.650: W/System.err(17525): com. Android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 11-15 13:59:04.650: W/System.err(17525): dalvik.system.NativeStart.main(ネイティブ メソッド) でmain(ネイティブメソッド)main(ネイティブメソッド)

4

1 に答える 1

2

間違ったInputStreamクラスをインポートしたと思います。InputStreamアプリのどこかでクラスを定義したようです。 のクラスを使用してくださいjava.io

于 2012-11-15T03:27:10.043 に答える