上传图像时出现空点异常

时间:2016-03-17 22:19:12

标签: android

我是Android开发的新手,我想将图像上传到在线服务器,但是当我上传图像时,它会抛出一个零点异常。有什么建议?请帮我 。我会非常感谢你。 这是我的DoInBackground代码

private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

private void uploadImage(){
    class UploadImage extends AsyncTask<Bitmap,Void,String> {
        public static final String UPLOAD_URL = "http://www.example/folder/upload.php";

        ProgressDialog loading;
        DBIMAGE rh = new DBIMAGE();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(Profile.this, "Uploading...", null,true,true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
          //  Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Bitmap... params) {
            Bitmap bitmap = params[0];

            String uploadImage = getStringImage(bitmap);


            HashMap<String,String> data = new HashMap<>();
            String name1 = data.get(SessionManager.KEY_NAME);


            data.put("image", uploadImage);
            data.put("name", name1);

            String result = rh.sendPostRequest(UPLOAD_URL,data);

            return result;
        }
    }

    UploadImage ui = new UploadImage();
    ui.execute(bitmap);
}

这是我的DBIMAGE文件代码

public class DBIMAGE {

public String sendPostRequest(String requestURL,
                              HashMap<String, String> postDataParams) {

    URL url;

    StringBuilder sb = new StringBuilder();
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            sb = new StringBuilder();
            String response;
            while ((response = br.readLine()) != null){
                sb.append(response);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}
}

和LOGCAT

03-18 02:56:53.250 23730-23730/? D/dalvikvm: Late-enabling CheckJNI
03-18 02:56:54.070 23730-23730/com.example.mubbasher.howdy_sendgreetings I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
03-18 02:56:54.070 23730-23730/com.example.mubbasher.howdy_sendgreetings W/dalvikvm: VFY: unable to resolve virtual method 390: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-18 02:56:54.070 23730-23730/com.example.mubbasher.howdy_sendgreetings D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-18 02:56:54.070 23730-23730/com.example.mubbasher.howdy_sendgreetings I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
03-18 02:56:54.080 23730-23730/com.example.mubbasher.howdy_sendgreetings W/dalvikvm: VFY: unable to resolve virtual method 412: Landroid/content/res/TypedArray;.getType (I)I
03-18 02:56:54.080 23730-23730/com.example.mubbasher.howdy_sendgreetings D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-18 02:56:54.300 23730-23730/com.example.mubbasher.howdy_sendgreetings D/OpenGLRenderer: Enabling debug mode 0
03-18 02:57:01.130 23730-23730/com.example.mubbasher.howdy_sendgreetings W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
03-18 02:57:01.160 23730-23730/com.example.mubbasher.howdy_sendgreetings W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
03-18 02:57:01.180 23730-23730/com.example.mubbasher.howdy_sendgreetings W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
03-18 02:57:01.220 23730-23730/com.example.mubbasher.howdy_sendgreetings W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
03-18 02:57:12.450 23730-23730/com.example.mubbasher.howdy_sendgreetings W/ResourceType: Failure getting entry for 0x01080a48 (t=7 e=2632) in package 0 (error -75)
03-18 02:57:12.450 23730-23730/com.example.mubbasher.howdy_sendgreetings W/ResourceType: Failure getting entry for 0x01080a60 (t=7 e=2656) in package 0 (error -75)
03-18 02:57:12.450 23730-23730/com.example.mubbasher.howdy_sendgreetings W/ResolverActivity: mLaunchedFromPackage=com.example.mubbasher.howdy_sendgreetings
03-18 02:57:17.030 23730-23730/com.example.mubbasher.howdy_sendgreetings D/AbsListView: onDetachedFromWindow
03-18 02:57:22.190 23730-23730/com.example.mubbasher.howdy_sendgreetings W/ResourceType: Failure getting entry for 0x01080a60 (t=7 e=2656) in package 0 (error -75)
03-18 02:57:22.370 23730-23730/com.example.mubbasher.howdy_sendgreetings D/ProgressBar: updateDrawableBounds: left = 0
03-18 02:57:22.370 23730-23730/com.example.mubbasher.howdy_sendgreetings D/ProgressBar: updateDrawableBounds: top = 0
03-18 02:57:22.370 23730-23730/com.example.mubbasher.howdy_sendgreetings D/ProgressBar: updateDrawableBounds: right = 48
03-18 02:57:22.370 23730-23730/com.example.mubbasher.howdy_sendgreetings D/ProgressBar: updateDrawableBounds: bottom = 48
03-18 02:57:52.670 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err: java.lang.NullPointerException
03-18 02:57:52.840 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at libcore.net.UriCodec.encode(UriCodec.java:132)
03-18 02:57:52.890 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at java.net.URLEncoder.encode(URLEncoder.java:57)
03-18 02:57:52.920 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at com.example.mubbasher.howdy_sendgreetings.DBIMAGE.getPostDataString(DBIMAGE.java:75)
03-18 02:57:53.050 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at com.example.mubbasher.howdy_sendgreetings.DBIMAGE.sendPostRequest(DBIMAGE.java:42)
03-18 02:57:53.060 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at com.example.mubbasher.howdy_sendgreetings.Profile$1UploadImage.doInBackground(Profile.java:293)
03-18 02:57:53.070 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at com.example.mubbasher.howdy_sendgreetings.Profile$1UploadImage.doInBackground(Profile.java:260)
03-18 02:57:53.090 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-18 02:57:53.100 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-18 02:57:53.140 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-18 02:57:53.160 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-18 02:57:53.170 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-18 02:57:53.170 23730-24594/com.example.mubbasher.howdy_sendgreetings W/System.err:     at java.lang.Thread.run(Thread.java:841)
03-18 02:57:53.700 23730-23730/com.example.mubbasher.howdy_sendgreetings I/Choreographer: Skipped 30 frames!  The application may be doing too much work on its main thread.
03-18 02:57:53.850 23730-23730/com.example.mubbasher.howdy_sendgreetings E/ViewRootImpl: sendUserActionEvent() mView == null
03-18 02:58:40.820 23730-23730/com.example.mubbasher.howdy_sendgreetings D/dalvikvm: GC_EXPLICIT freed 3192K, 40% free 5590K/9316K, paused 2ms+6ms, total 74ms
03-18 03:00:41.030 23730-23730/com.example.mubbasher.howdy_sendgreetings D/dalvikvm: GC_EXPLICIT freed 235K, 43% free 5392K/9316K, paused 2ms+3ms, total 35ms

和PHP CODE

  <?php

  if($_SERVER['REQUEST_METHOD']=='POST'){

 $image = $_POST['image'];
 $name = $_POST['name'];

 $db_name = "j";
 $mysql_user = "root";
 $mysql_pass = "";
 $server_name = "localhost";

 $con = mysqli_connect($server_name, $mysql_user, $mysql_pass, $db_name);

 $sql ="SELECT id FROM photos ORDER BY id ASC";

 $res = mysqli_query($con,$sql);

 $id = 0;

  while($row = mysqli_fetch_array($res)){
  $id = $row['id'];
 }

 $path = "uploads/$id.png";

 $actualpath = "http://kkkkk/Howdy/$path";

  $sql = "UPDATE user_info (image) VALUES ('$actualpath') WHERE name='$name'";

 if(mysqli_query($con,$sql)){
  file_put_contents($path,base64_decode($image));
 echo "Successfully Uploaded";
 }

 mysqli_close($con);
 }else{
 echo "Error";
 }
 ?>

0 个答案:

没有答案
相关问题