我正在上传图片。当我在我的设备上从 eclipse 运行应用程序时,它运行良好并且上传文件非常好。当我关闭应用程序并从设备菜单启动它,然后当我尝试上传图像时,它给了我 RuntimeException 错误。我不知道是怎么回事。请帮忙
这是我的代码
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();
}
}
}
这是我的日志猫
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)