2015-04-23 64 views
-1

我有一個奇怪的問題。我正在開發一個具有3個獨立異步任務的Android應用程序。當我嘗試在四核手機上運行應用程序時,沒有任何問題。但如果我嘗試在雙核手機應用程序崩潰時運行應用程序。我如何修改我的雙核手機任務?Android應用程序中的多個異步任務

這裏是我的代碼

OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); 

這是第一個異步任務。第二個是Parse.com文件上傳異步saveInBackground方法。

public void startUpload(String fileName) { 

     try { 
      photoFile = new ParseFile(fileName, scaledData); 
      if (isTac) { 
       pictures.setPhotoFileTac(photoFile); 
      } else if (isCanak) { 
       pictures.setPhotoFileCanak(photoFile); 
      } else if (isYaprak) { 
       pictures.setPhotoFileYaprak(photoFile); 
      } 


      // pictures.save();// Telefon çekirdeğine göre 2 asenkron methodu desteklemiyor o yüzden sadece save yazılabilir fakat başarılı kontolü SaveCallback' te yakalanamaz. 




      pictures.saveInBackground(new SaveCallback() { 

       @Override 
       public void done(ParseException e) { 
        if(e == null){ 
         Toast.makeText(getApplicationContext(),"Buluta yükleme başarılı. " , Toast.LENGTH_LONG).show(); 
         if(pdialog != null) 
         { 
          pdialog.dismiss();//Eğer işlem başarılı ise asenkron sınıfta yaratılan progressbar ı kapat. 
         } 
        } 
        else{ 
         Toast.makeText(getApplicationContext(),"Hata" +e.toString(),Toast.LENGTH_LONG).show(); 
        } 
       } 
      }); 

     } 
     catch (Exception ex) 
     { 
      Toast.makeText(getApplicationContext(),"Bağlantı Hatası !",Toast.LENGTH_LONG).show(); 
     } 
    } 

第三個是上傳過程的進度條。

public class AsyncUpload extends AsyncTask<String,Void,String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pdialog = new ProgressDialog(TakePictureActivity.this); 
     pdialog.setMessage("Yükleniyor..."); 
     pdialog.setIndeterminate(false); 
     pdialog.setCancelable(false); 
     pdialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... fileNames) { 

     return fileNames[0]; 

    } 

    @Override 
    protected void onPostExecute(String name) { 

     startUpload(name); 

     super.onPostExecute(name); 

    } 

} 

還有我初始化上傳會話的代碼。 (在如果 - else條件狀態行新AsyncUpload()執行(文件名);

private void saveScaledPhoto(byte[] data) { 

    // Resize photo from camera byte array 
    pictureWidth = camera.getParameters().getPictureSize().width; 
    pictureHeight = camera.getParameters().getPictureSize().height; 
    Bitmap plantImage = BitmapFactory.decodeByteArray(data, 0, data.length); 
    Bitmap plantImageScaled = Bitmap.createScaledBitmap(plantImage, pictureWidth, pictureHeight, false); 
    pictureCache = new PictureCache(); 
    // Override Android default landscape orientation and save portrait 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(90); 
    Bitmap rotatedScaledPlantImage = Bitmap.createBitmap(plantImageScaled, 0, 
      0, plantImageScaled.getWidth(), plantImageScaled.getHeight(), 
      matrix, true); 

    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    rotatedScaledPlantImage.compress(Bitmap.CompressFormat.PNG, 100, bos); 

    scaledData = bos.toByteArray(); 

    AlertDialog.Builder aDB = new AlertDialog.Builder(this); 
    aDB.setCancelable(false); 
    aDB.setTitle("Emin misiniz ?"); 
    aDB.setMessage("Çektiğiniz resim analizde kullanılacaktır. Devam etmek istiyor musunuz ?.. "); 
    aDB.setPositiveButton("Evet", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

      if (isTac) { 

       pictureCache.setByteArrayTac(scaledData); 
       isTac = false; 
       isCanak = true; 
       Toast.makeText(getApplicationContext(), "Taç yaprak görüntüsü alındı.", Toast.LENGTH_LONG).show(); 
       String currentTimeStamp = getCurrentTimeStamp(); 
       fileName = "TacYaprak"; 

       new AsyncUpload().execute(fileName); 

      } else if (isCanak) { 


       pictureCache.setByteArrayCanak(scaledData); 
       isCanak = false; 
       isYaprak = true; 
       Toast.makeText(getApplicationContext(), "Çanak yaprak görüntüsü alındı.", Toast.LENGTH_LONG).show(); 
       String currentTimeStamp = getCurrentTimeStamp(); 
       fileName = "CanakYaprak"; 

       new AsyncUpload().execute(fileName); 
      } else if (isYaprak) { 

       String plantTag = "A_Y"; 
       pictureCache.setByteArrayYaprak(scaledData); 
       isYaprak = false; 
       Toast.makeText(getApplicationContext(), "Ağaç yaprağı görüntüsü alındı.", Toast.LENGTH_LONG).show(); 
       String currentTimeStamp = getCurrentTimeStamp(); 
       fileName = "AgacYapragi"; 

       new AsyncUpload().execute(fileName); 
      } 

      if (!isTac && !isCanak && !isYaprak) { 
       finish(); 
      } 

     } 


    }); 
    aDB.setNegativeButton("Hayır", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 
    AlertDialog alertDialog = aDB.create(); 
    alertDialog.show(); 
} 

這裏是logcat的輸出..

java.lang.NullPointerException 
     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:482) 
     at com.altygtsoft.biomatch.TakePictureActivity.saveScaledPhoto(TakePictureActivity.java:202) 
     at com.altygtsoft.biomatch.TakePictureActivity.access$000(TakePictureActivity.java:45) 
     at com.altygtsoft.biomatch.TakePictureActivity$2$1$2.onPictureTaken(TakePictureActivity.java:147) 
     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:855) 
     at android.os.Handler.dispatchMessage(Handler.java:107) 
     at android.os.Looper.loop(Looper.java:194) 
     at android.app.ActivityThread.main(ActivityThread.java:5371) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:525) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
     at dalvik.system.NativeStart.main(Native Method) 
+2

請張貼stacktra會發生什麼ce錯誤 – AndroidEx

+0

您是否還會發布實際執行AsyncTask的行? – beyondtheteal

+0

您是否收到內存不足錯誤? –

回答

0

剛剛意識到我的想法是太大註釋所謂>

如果您執行以下操作?

//Beware pseudo-code 
new AsyncTask<>({ 
    onPreExecute(){ 
     //keep your code from before 
     ... 
    } 
    doInBackground(Params... params){ 
     startUpload(params[0], params[1]); //minus the done() function, plus pictures? 
    } 
    onPostExecute(){ 
    //As I'm not sure where it fits in, if possible execute done() here. 
    } 
}).execute(fileName, pictures); //couldn't find the decleration of pictures, so I stole the decleration of independence instead. Also maybe you need to put pictures in there, depending on where it is and how public/static. 
+0

感謝您的貢獻我決定在其他活動中進行圖像處理,基本上我已經註釋掉了OpenCV.initAsync行和問題,但我的AsyncTask的定義存在我知道的問題,但目前看不到它:) –