Android App中的多个异步任务

时间:2015-04-23 21:08:00

标签: android asynchronous

我有一个奇怪的问题。我正在开发一个Android应用程序,它有3个独立的异步任务。当我尝试在四核手机上运行应用程序时没有问题。但如果我尝试在双核手机应用程序上运行应用程序崩溃。如何修改双核手机的任务?

这是我的代码

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);

    }

}

还有我初始化上传会话的代码。 (在if - else条件中声明行 new AsyncUpload()。execute(fileName);

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)

1 个答案:

答案 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.