将位图图像转换为字节数组,然后将其发送到主线程

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

标签: android bitmap android-asynctask

我试图在onpostexecuteasync task class的结果之后将位图图像转换为字节数组。结果在onpostexecute中正确设置,但是当它到达转换部分(从位图转换为字节数组)时,它会抛出错误。

这是on create方法

 ...
public byte[]  ImageArray;
public  Bitmap ImageBitmap;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //This instantiate the async class
    DownloadImageTask asyncTask =new  DownloadImageTask(new AsyncResponse() {

        @Override
        public void processFinish(Bitmap output) {

            ImageBitmap = output;
        }
    });

    asyncTask.execute(imageurl);


    //I want to be able to get the ImageArray on the main thread after the async task execution
        ....
}



/**
 * This sub class downloads the attached  image
 * file to be viewed on the page.
 */
private class  DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

    public AsyncResponse delegate=null;


    public DownloadImageTask(AsyncResponse asyncResponse) {
        delegate = asyncResponse; //Assigning call back interfacethrough constructor
    }


    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);


        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {

        delegate.processFinish(result);


        //This converts the bitmap to an byte array 
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        result.compress(Bitmap.CompressFormat.PNG, 50, stream);
        ImageArray = stream.toByteArray();
    }
}


//This creates an interface for the results from the async task
public interface AsyncResponse {
    void processFinish(Bitmap output);
}

请问是否可以将位图图像转换为onPostExecute中的字节数组并在主线程上获取ImageArray?如果是的话,请问我该怎么做,因为我做了什么使应用程序崩溃。谢谢你的帮助

更新

请将logcat中的错误添加到问题

  

8334-8334 / com.myaapp.onookow E / AndroidRuntime:致命异常:主要       显示java.lang.NullPointerException               在com.myaapp.onookow.onokows $ DownloadImageTask.onPostExecute(onokows.java:342)               在com.myaapp.onookow.onokows $ DownloadImageTask.onPostExecute(onokows.java:311)               在android.os.AsyncTask.finish(AsyncTask.java:417)               在android.os.AsyncTask.access $ 300(AsyncTask.java:127)               在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:429)               在android.os.Handler.dispatchMessage(Handler.java:99)               在android.os.Looper.loop(Looper.java:130)               在android.app.ActivityThread.main(ActivityThread.java:3687)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:507)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)               在dalvik.system.NativeStart.main(Native Method)。

STRANGE UPDATE

在具有API&gt;的设备上测试应用程序之后11,每件事都有效。但是在具有API&lt; 11,每件事都开始工作,但在几秒钟内,应用程序会冻结此流行图像,请查看下面的

enter image description here

2 个答案:

答案 0 :(得分:0)

该行:

delegate.processFinish(result);

应该放在:

之后
//This converts the bitmap to an byte array 
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageBitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
ImageArray = stream.toByteArray();

否则在processFinish()

中ImageArray == null

答案 1 :(得分:0)

在下面的函数中 -

  protected void onPostExecute(Bitmap result) {

    delegate.processFinish(result);


    //This converts the bitmap to an byte array 
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ImageBitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
    ImageArray = stream.toByteArray();
}

}

参数是Bitmap类型的结果。 但是你没有在代码中的任何地方使用结果。我想知道如何从位图转换到bytearray? 我认为您没有将结果位图设置为任何流。