0
我正在上傳圖片。當我在我的設備上從eclipse運行應用程序時,它運行良好並且上傳文件非常好。當我關閉應用程序,並從設備菜單啓動它,然後當我嘗試上傳圖像時,它給我RuntimeException錯誤。我不知道發生了什麼事。請幫助上傳圖片時出現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();
}
}
}
,這裏是我的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 [email protected] 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 [email protected] 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)
ava.lang.OutOfMemoryError:位圖大小超過VM預算,搜索會得到很多的幫助 –
http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android –
http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966將幫助你 – duffy356