0

I am uploading an image. When I run application from eclipse on my device it runs fine and uploads file perfectly fine. When I close app and start it from device menu and then when I try to upload image it gives me RuntimeException error. I have no idea whats going on. Help please

here is my code

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;
String largeImagePath;

int serverResponseCode = 0;

@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";
         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.
             largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));

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

             photoName = f.getName();

             bUpload.setOnClickListener(new View.OnClickListener() {   
                 public void onClick(View v) {
                     new Upload().execute(largeImagePath, globalUID, photoName);
                 }

             });

         } finally {
             myCursor.close();
         }
    }
}
public class Upload extends AsyncTask<String, Integer, String> {

    ProgressDialog dialog;

    protected void onPreExecute() {
        dialog = ProgressDialog.show(Camera.this, "", "Uploading file...", true);
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String success = "false";
        Bitmap bitmapOrg = BitmapFactory.decodeFile(largeImagePath);
        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("imageName", photoName));
        nameValuePairs.add(new BasicNameValuePair("uid", globalUID));

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.example.info/android/fileupload.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;
    }

    protected void onProgressUpdate(Integer...progress) {

    }

    protected void onPostExecute(String f) {
        Toast.makeText(getApplicationContext(), "File uploaded", Toast.LENGTH_LONG).show();
    }

}

}

here is my logcat

06-23 14:47:50.830: E/AndroidRuntime(7069): FATAL EXCEPTION: AsyncTask #4
06-23 14:47:50.830: E/AndroidRuntime(7069): java.lang.RuntimeException: An error occured while executing doInBackground()
06-23 14:47:50.830: E/AndroidRuntime(7069):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at java.lang.Thread.run(Thread.java:1019)
06-23 14:47:50.830: E/AndroidRuntime(7069): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-23 14:47:50.830: E/AndroidRuntime(7069):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:573)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:384)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:412)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at com.zafar.login.Camera$Upload.doInBackground(Camera.java:122)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at com.zafar.login.Camera$Upload.doInBackground(Camera.java:1)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-23 14:47:50.830: E/AndroidRuntime(7069):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-23 14:47:50.830: E/AndroidRuntime(7069):     ... 4 more
06-23 14:47:56.280: E/WindowManager(7069): Activity com.zafar.login.DashboardActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40a67df0 that was originally added here
06-23 14:47:56.280: E/WindowManager(7069): android.view.WindowLeaked: Activity com.zafar.login.DashboardActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40a67df0 that was originally added here
06-23 14:47:56.280: E/WindowManager(7069):  at android.view.ViewRoot.<init>(ViewRoot.java:266)
06-23 14:47:56.280: E/WindowManager(7069):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:174)
06-23 14:47:56.280: E/WindowManager(7069):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
06-23 14:47:56.280: E/WindowManager(7069):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
06-23 14:47:56.280: E/WindowManager(7069):  at android.app.Dialog.show(Dialog.java:241)
06-23 14:47:56.280: E/WindowManager(7069):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
06-23 14:47:56.280: E/WindowManager(7069):  at android.app.ProgressDialog.show(ProgressDialog.java:90)
06-23 14:47:56.280: E/WindowManager(7069):  at com.zafar.login.Camera$Upload.onPreExecute(Camera.java:115)
06-23 14:47:56.280: E/WindowManager(7069):  at android.os.AsyncTask.execute(AsyncTask.java:391)
06-23 14:47:56.280: E/WindowManager(7069):  at com.zafar.login.Camera$1.onClick(Camera.java:100)
06-23 14:47:56.280: E/WindowManager(7069):  at android.view.View.performClick(View.java:2538)
06-23 14:47:56.280: E/WindowManager(7069):  at android.view.View$PerformClick.run(View.java:9152)
06-23 14:47:56.280: E/WindowManager(7069):  at android.os.Handler.handleCallback(Handler.java:587)
06-23 14:47:56.280: E/WindowManager(7069):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-23 14:47:56.280: E/WindowManager(7069):  at android.os.Looper.loop(Looper.java:130)
06-23 14:47:56.280: E/WindowManager(7069):  at android.app.ActivityThread.main(ActivityThread.java:3691)
06-23 14:47:56.280: E/WindowManager(7069):  at java.lang.reflect.Method.invokeNative(Native Method)
06-23 14:47:56.280: E/WindowManager(7069):  at java.lang.reflect.Method.invoke(Method.java:507)
06-23 14:47:56.280: E/WindowManager(7069):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
06-23 14:47:56.280: E/WindowManager(7069):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
06-23 14:47:56.280: E/WindowManager(7069):  at dalvik.system.NativeStart.main(Native Method)
4
  • 1
    ava.lang.OutOfMemoryError: bitmap size exceeds VM budget, search this will get a lot of help Commented Jun 23, 2012 at 12:00
  • 1
    stackoverflow.com/questions/2928002/… Commented Jun 23, 2012 at 12:01
  • 1
    stackoverflow.com/questions/477572/… will help you Commented Jun 23, 2012 at 12:07
  • @DheereshSingh and duffy56 How can I use that method on that link in my code. I am trying that but that one is used for files not for images. Could you modify my code with that method? thanks in advance Commented Jun 23, 2012 at 12:19

1 Answer 1

0

Solution

First of all with the help of @DheereshSingh and @duffy356 comments. I found it solution on this link.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.