1
  1. 我正在尝试制作一个应用程序,该应用程序在按下按钮时从相机拍摄照片并首先将其显示在 a 上SurfaceView,然后.php file通过互联网将其发送到 a。我的代码给出了“http 连接 java.net.UnknownHostException 中的错误:sibot.bugs3.com”。

  2. 我的 MainActivity.java 文件如下

    public class MainActivity extends Activity {
        Button btn;
        ImageView img;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btn=(Button) findViewById(R.id.button1);
            img=(ImageView) findViewById(R.id.sv);
            btn.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    CaptureImage(img);
                }
            });
        }
    
        private void CaptureImage( final ImageView iv_image) {
    
            int FrontCameraFound = getCameraID();
            if (FrontCameraFound != -1) {
                final Camera mCamera = Camera.open(FrontCameraFound);
    
                Parameters parameters = mCamera.getParameters();
    
                mCamera.setParameters(parameters);
                mCamera.startPreview();
    
                Camera.PictureCallback mCall = new Camera.PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {
                        InputStream is;
                        Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
    
                        // set bitmap tp image view just to check 
                        // if image capture proper, testing purpose
                        iv_image.setImageBitmap(bmp);
    
                        mCamera.stopPreview();
                        mCamera.release();
                      //  mCamera = null;
                      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();                     
                        String bal;
                    try{  ByteArrayOutputStream bao = new ByteArrayOutputStream();
    
                        bmp.compress(Bitmap.CompressFormat.JPEG, 90, bao);
    
                        byte [] ba = bao.toByteArray();
    
                        bal=Base64.encodeBytes(ba);
                         nameValuePairs.add(new BasicNameValuePair("image",bal));
                        }catch(Exception e){ Log.e("ERROR", "caught one "+ e.toString());}
                      try{
    
                          HttpClient httpclient = new DefaultHttpClient();
    
                          HttpPost httppost = new HttpPost("http://sibot.bugs3.com/pic.php");
    
                          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
                          HttpResponse response = httpclient.execute(httppost);
    
                          HttpEntity entity = response.getEntity();
    
                          is = entity.getContent();
    
                          }catch(Exception e){
    
                          Log.e("log_tag", "Error in http connection "+e.toString());
    
                          }
                    }
                };
                mCamera.takePicture(null, null, mCall);
            }
        }
    
        private int getCameraID() {
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    
            for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); camIdx++) {
                Camera.getCameraInfo(camIdx, cameraInfo);
                // for capture image from back camera
                // If want to capture from front 
                // then change it to CAMERA_FACING_FRONT
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                    try {
                        return camIdx;
                    } catch (RuntimeException e) {
    
                    }
                }
            }
            return -1;
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    
  3. 我的清单文件如下

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.android.img2"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="9"
            android:targetSdkVersion="9" />
         <uses-permission android:name="android.permission.CAMERA" />
            <uses-feature android:name="android.hardware.camera" />
            <uses-feature android:name="android.hardware.INTERNET" />
            <uses-feature android:name="android.hardware.ACCESS_NETWORK_STATE" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.android.img2.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>`enter code here`
            <activity android:name="com.android.img2.Base64" />
        </application>
    </manifest>
    

即使包含所有权限并确保我手机上的网络已打开,我也无法解决问题。给予的任何帮助将不胜感激。

4

0 回答 0