在位图压缩和保存异步任务期间出现问题

时间:2013-10-23 17:04:42

标签: android bitmap android-asynctask android-memory

我正在使用自定义相机设备编写应用程序。在捕获后我的自定义相机我将捕获的图像绘制到画布上,并提供捕获的imgae上的免费手绘,然后保存选项。在保存时我将它保存为两个图像,这意味着包含自由手绘图的图像和另一个不包含绘图的图像。通过编写输出流和压缩位图来完成保存。在两个单独的异步任务中完成位图的保存和压缩。问题是我可以捕获图像达16或17次但在捕获和编辑之后然后按下保存按钮我得到异常“vm abaling Fatal signal 11(SIGSEGV)at 0xdeadd00d(code = 1)”。 异步任务一

public class SaveOriginalImage extends AsyncTask<String, Void, String> {
    OutputStream dataOutputStream;
    Bitmap bitMapOriginalImage;
    String fileName;
    Activity activityContext;
    ProgressDialog progressDialog;
    String sbCaption;
    String fileType;

    public SaveOriginalImage(Bitmap bitMap, String filePath,
            Activity currentActivity, String fileCaption) {
        this.bitMapOriginalImage = bitMap;
        this.fileName = filePath;
        this.activityContext = currentActivity;
        this.sbCaption = fileCaption;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            dataOutputStream = new FileOutputStream(fileName);

            bitMapOriginalImage
                    .compress(CompressFormat.PNG, 100, dataOutputStream);
            Collection.lastImageFilePath = fileName;

            dataOutputStream.flush();
            dataOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();

    }

    @Override
    protected void onPostExecute(String result) {
        if (bitMapOriginalImage != null) {
            bitMapOriginalImage.recycle();
            bitMapOriginalImage = null;
        }
    }
}

异步任务2

public class SaveFreeHandImage extends AsyncTask<String, Void, String> {
OutputStream dataOutputStream;
Bitmap bitMapToSave;
String fileName;
Activity activityContext;
ProgressDialog progressDialog;
String sbCaption;
String className;
String fileType;

public SaveFreeHandImage(Bitmap bitMap, String filePath,
        Activity currentActivity, String fileCaption, String className) {
    this.bitMapToSave = bitMap;
    this.fileName = filePath;
    this.activityContext = currentActivity;
    this.sbCaption = fileCaption;
    this.className = className;
}

@Override
protected String doInBackground(String... params) {
    try {

        dataOutputStream = new FileOutputStream(fileName);

        bitMapToSave.compress(CompressFormat.PNG, 100, dataOutputStream);
        Collection.lastImageFilePath = fileName;
        try {
            dataOutputStream.flush();
            dataOutputStream.close();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onPostExecute(String result) {
    // super.onPostExecute(result);
    progressDialog.dismiss();

    HomeFinal.showCustomToast("Drawing saved to SD card ", 0, 0,
            activityContext);
    Collection.isNewImageAdded = false;
    DrawingView.colorD = Color.parseColor("#000000");
    if (DrawingView.paths != null) {
        if (DrawingView.paths.size() >= 1) {
            DrawingView.paths.clear();
        }
    }

    if (bitMapToSave != null) {
        if (!bitMapToSave.isRecycled()) {
            bitMapToSave.recycle();
            bitMapToSave = null;
        }
    }
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog = ProgressDialog.show(activityContext, "", "Saving..");
    }
 }

我正在测试联想a-300h 7英寸平板电脑。请给我解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:3)

我解决了自己。这是因为过量使用位图后,处理位图后效果很好。