1

我正在上传图像并发送一些参数。这样做时,我的 logcat 中出现错误。

这是我的代码

public class Camera extends Activity {
ImageView ivUserImage;
Button bUpload;
Intent i;
int CameraResult = 0;
Bitmap bmp;
public String globalUID;
UserFunctions userFunctions;
String photoName;
InputStream is;

int serverResponseCode = 0;
ProgressDialog dialog = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);

    userFunctions = new UserFunctions();
    globalUID = getIntent().getExtras().getString("globalUID");
    Toast.makeText(getApplicationContext(), globalUID, Toast.LENGTH_LONG).show();
    ivUserImage = (ImageView)findViewById(R.id.ivUserImage);
    bUpload = (Button)findViewById(R.id.bUpload);
    openCamera();
}

private void openCamera() {
    i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(i, CameraResult);
}

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

    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK) {
        //set image taken from camera on ImageView
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        ivUserImage.setImageBitmap(bmp);

        //Create new Cursor to obtain the file Path for the large image
         String[] largeFileProjection = {
                 MediaStore.Images.ImageColumns._ID,
                 MediaStore.Images.ImageColumns.DATA
         };

         String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
         //final String photoName = MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME;
         Cursor myCursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);

         try {
             myCursor.moveToFirst();
             //This will actually give you the file path location of the image.
             final String largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
             //final String photoName = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME));
             //Log.e("URI", largeImagePath);

             File f = new File("" + largeImagePath);

             photoName = f.getName();

             bUpload.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        dialog = ProgressDialog.show(Camera.this, "", "Uploading file...", true);
                         new Thread(new Runnable() {
                                public void run() {
                                     runOnUiThread(new Runnable() {
                                        public void run() {
                                            //tv.setText("uploading started.....");
                                            Toast.makeText(getBaseContext(), "File is uploading...", Toast.LENGTH_LONG).show();
                                        }
                                     });
                                 if(imageUpload(globalUID, largeImagePath)) {
                                     Toast.makeText(getApplicationContext(), "Image uploaded", Toast.LENGTH_LONG).show();
                                 } else {
                                     Toast.makeText(getApplicationContext(), "Error uploading image", Toast.LENGTH_LONG).show();
                                 }
                                 //Log.e("Response: ", response);
                                 //System.out.println("RES : " + response);
                                }
                              }).start();
                        }
                });
         } finally {
             myCursor.close();
         }
         //Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
         //String imageUrl = getRealPathFromURI(uriLargeImage);
    }
}

public boolean imageUpload(String uid, String imagepath) {
    boolean success = false;
    //Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    Bitmap bitmapOrg = BitmapFactory.decodeFile(imagepath);
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

    byte [] ba = bao.toByteArray();
    String ba1=Base64.encodeToString(ba, 0);

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("image",ba1));
    nameValuePairs.add(new BasicNameValuePair("uid", uid));

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.example.info/androidfileupload/index.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        if(response != null) {
            success = true;
        }
        is = entity.getContent();
    } catch(Exception e) {
        Log.e("log_tag", "Error in http connection "+e.toString());
    }
    dialog.dismiss();
    return success;
}

这是我的日志猫

06-23 11:54:48.555: E/AndroidRuntime(30601): FATAL EXCEPTION: Thread-11
06-23 11:54:48.555: E/AndroidRuntime(30601): java.lang.OutOfMemoryError
06-23 11:54:48.555: E/AndroidRuntime(30601):    at java.lang.String.<init>(String.java:513)
06-23 11:54:48.555: E/AndroidRuntime(30601):    at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:650)
06-23 11:54:48.555: E/AndroidRuntime(30601):    at java.lang.StringBuilder.toString(StringBuilder.java:664)
06-23 11:54:48.555: E/AndroidRuntime(30601):    at org.apache.http.client.utils.URLEncodedUtils.format(URLEncodedUtils.java:170)
06-23 11:54:48.555: E/AndroidRuntime(30601):    at org.apache.http.client.entity.UrlEncodedFormEntity.<init>(UrlEncodedFormEntity.java:71)
06-23 11:54:48.555: E/AndroidRuntime(30601):    at com.zafar.login.Camera.imageUpload(Camera.java:155)
06-23 11:54:48.555: E/AndroidRuntime(30601):    at com.zafar.login.Camera$1$1.run(Camera.java:119)
06-23 11:54:48.555: E/AndroidRuntime(30601):    at java.lang.Thread.run(Thread.java:1019)
06-23 11:54:53.960: E/WindowManager(30601): Activity com.zafar.login.DashboardActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405db540 that was originally added here
06-23 11:54:53.960: E/WindowManager(30601): android.view.WindowLeaked: Activity com.zafar.login.DashboardActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405db540 that was originally added here
06-23 11:54:53.960: E/WindowManager(30601):     at android.view.ViewRoot.<init>(ViewRoot.java:266)
06-23 11:54:53.960: E/WindowManager(30601):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:174)
06-23 11:54:53.960: E/WindowManager(30601):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
06-23 11:54:53.960: E/WindowManager(30601):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
06-23 11:54:53.960: E/WindowManager(30601):     at android.app.Dialog.show(Dialog.java:241)
06-23 11:54:53.960: E/WindowManager(30601):     at android.app.ProgressDialog.show(ProgressDialog.java:107)
06-23 11:54:53.960: E/WindowManager(30601):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
06-23 11:54:53.960: E/WindowManager(30601):     at com.zafar.login.Camera$1.onClick(Camera.java:110)
06-23 11:54:53.960: E/WindowManager(30601):     at android.view.View.performClick(View.java:2538)
06-23 11:54:53.960: E/WindowManager(30601):     at android.view.View$PerformClick.run(View.java:9152)
06-23 11:54:53.960: E/WindowManager(30601):     at android.os.Handler.handleCallback(Handler.java:587)
06-23 11:54:53.960: E/WindowManager(30601):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-23 11:54:53.960: E/WindowManager(30601):     at android.os.Looper.loop(Looper.java:130)
06-23 11:54:53.960: E/WindowManager(30601):     at android.app.ActivityThread.main(ActivityThread.java:3691)
06-23 11:54:53.960: E/WindowManager(30601):     at java.lang.reflect.Method.invokeNative(Native Method)
06-23 11:54:53.960: E/WindowManager(30601):     at java.lang.reflect.Method.invoke(Method.java:507)
06-23 11:54:53.960: E/WindowManager(30601):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
06-23 11:54:53.960: E/WindowManager(30601):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
06-23 11:54:53.960: E/WindowManager(30601):     at dalvik.system.NativeStart.main(Native Method)
06-23 11:54:55.190: I/Process(30601): Sending signal. PID: 30601 SIG: 9
4

1 回答 1

0

解决方案

添加try and catch内部线程后,它可以工作。这是我添加 try catch 的地方

public void run() {
       try {
                //Toast.makeText(getBaseContext(), "File is uploading...", Toast.LENGTH_LONG).show();
                if(imageUpload(globalUID, largeImagePath)) {
                     Toast.makeText(getApplicationContext(), "Image uploaded", Toast.LENGTH_LONG).show();
                } else {
                     Toast.makeText(getApplicationContext(), "Error uploading image", Toast.LENGTH_LONG).show();
                }
        } catch (Exception e) {

        }

}
于 2012-06-23T09:43:06.117 回答