使用Bitmap释放背景部分并发标记扫描GC

时间:2015-04-15 09:46:11

标签: java android json bitmap garbage-collection

我的应用正在解析JSON数据,使用此JSON数据,我更改了TextView文本,ImageButton图像。 一切正常,但在我的Logcat中,我有一些错误“背景部分并发标记扫描GC释放了Bitmap ”。这是一个垃圾收集问题吗?

我该如何解决? 干杯!

这是我的代码:

public class MainActivity extends ActionBarActivity {

    public static final String TAG = MainActivity.class.getSimpleName();
    private CurrentReportString mCurrentReportString;
    @InjectView(R.id.jsonText) TextView mJsonText;
    @InjectView(R.id.imageReport) ImageButton mImageReport;
    @InjectView(R.id.imageReport2) ImageButton mImageReport2;
    @InjectView(R.id.imageReport3) ImageButton mImageReport3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        String urlWebSite = "http://www.bundoransurfco.com/surf-report/surf-report/?json=1";

        if (isNetworkAvailable()) {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(urlWebSite)
                    .build();

            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {

                }

                @Override
                public void onResponse(Response response) throws IOException {
                    try {
                        String jsonData = response.body().string();
                        if (response.isSuccessful()) {
                            mCurrentReportString = getCurrentDetails(jsonData);
                            new DownloadImageTask(mImageReport)
                                    .execute(mCurrentReportString.getUrlImages());
                            ;

                            // Write in the main Thread.
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    updateReport();
                                }
                            });

                        } else {
                            alertUserAboutError();
                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Exception caught: ", e);
                    }
                      catch (JSONException e){
                          Log.e(TAG, "Exception caught: ", e);
                      }
                }
            });
        }
        else {
            Toast.makeText(this, getString(R.string.network_unavailable_message), Toast.LENGTH_LONG).show();
        }

        Log.d(TAG, "Main UI code is running!");
    }

    private void updateReport() {
        mJsonText.setText(mCurrentReportString.getJsonContent());
        if (mCurrentReportString.imageBackground == Boolean.FALSE)
        {
            Log.i(TAG, "No Background1");
            mImageReport.setVisibility(View.INVISIBLE);
        }
        if (mCurrentReportString.imageBackground2 == Boolean.FALSE)
        {
            Log.i(TAG, "No Background2");
            mImageReport2.setVisibility(View.INVISIBLE);
        }
        if (mCurrentReportString.imageBackground3 == Boolean.FALSE)
        {
            Log.i(TAG, "No Background3");
            mImageReport3.setVisibility(View.INVISIBLE);
        }

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private CurrentReportString getCurrentDetails(String jsonData) throws JSONException {
        JSONObject report = new JSONObject(jsonData);

        JSONObject postJson = report.getJSONObject("post");
        CurrentReportString currentReportString = new CurrentReportString();
        String contentString = postJson.getString("content");


        //Report String
        String contentWithoutWebcam = contentString.replace("CLICK HERE FOR LIVE PEAK WEBCAM", "");
        int lastCharacter = contentWithoutWebcam.indexOf("#gallery-1");
        String contentFirstPart = contentWithoutWebcam.substring(0, lastCharacter);
        String jsonText = stripHtml(contentFirstPart);
        currentReportString.setJsonContent(jsonText);

        //Images
        Document doc = Jsoup.parse(contentString);
        Elements relLinks = doc.select("a[rel]");
//Elements relLinks = doc.select("a[rel=prettyPhoto[gallery-113]]");//
        System.out.println("number of `rel`: "+relLinks.size());
        ArrayList<String> urls=new ArrayList<String>();
        for (Element el : relLinks){
         //   Log.i(TAG, el.attr("href"));
            urls.add(el.attr("href"));
        }
        if (relLinks.size() == 0){
            Log.i(TAG, "0 image");
            currentReportString.setImageBackground(Boolean.FALSE);
            currentReportString.setImageBackground2(Boolean.FALSE);
            currentReportString.setImageBackground3(Boolean.FALSE);
        }
        else if (relLinks.size() == 1) {
            // Log.i(TAG, String.valueOf(urls));
            currentReportString.setUrlImages(urls.get(0));
            Log.i(TAG, "first URL : " + currentReportString.getUrlImages());
            currentReportString.setImageBackground2(Boolean.FALSE);
            currentReportString.setImageBackground3(Boolean.FALSE);
        }
        else if (relLinks.size() == 2){
            currentReportString.setUrlImages(urls.get(0));
            currentReportString.setUrlImages2(urls.get(1));
            currentReportString.setImageBackground3(Boolean.FALSE);
            Log.i(TAG, "first URL : "+currentReportString.getUrlImages());
            Log.i(TAG, "second URL : "+currentReportString.getUrlImages2());
        }
        else if (relLinks.size() == 3){
            currentReportString.setUrlImages(urls.get(0));
            currentReportString.setUrlImages2(urls.get(1));
            currentReportString.setUrlImages2(urls.get(2));
            Log.i(TAG, "first URL : "+currentReportString.getUrlImages());
            Log.i(TAG, "second URL : "+currentReportString.getUrlImages2());
            Log.i(TAG, "third URL : "+currentReportString.getUrlImages3());
        }
        else if (relLinks.size() > 3){
            currentReportString.setUrlImages(urls.get(0));
            currentReportString.setUrlImages2(urls.get(1));
            currentReportString.setUrlImages2(urls.get(2));
            Log.i(TAG, "first URL : "+currentReportString.getUrlImages());
            Log.i(TAG, "second URL : "+currentReportString.getUrlImages2());
            Log.i(TAG, "third URL : "+currentReportString.getUrlImages3());
        }


        return currentReportString;
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()) {
            isAvailable = true;
        }

        return isAvailable;
    }
    //Delete HTML Markup
    public String stripHtml(String html) {
        return Html.fromHtml(html).toString();
    }

    private void alertUserAboutError() {
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.show(getFragmentManager(), "error_dialog");
    }

    //Change Image from ImageButton in Asynch with URL.
    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }
        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) {
            bmImage.setImageBitmap(result);
        }
    }
}

这是我的Logcat:

04-15 10:32:15.203    3008-3020/jardelcompany.bundoransurfco W/art﹕ Suspending all threads took: 15.897ms
04-15 10:32:15.257    3008-3020/jardelcompany.bundoransurfco I/art﹕ Background partial concurrent mark sweep GC freed 91(16KB) AllocSpace objects, 0(0B) LOS objects, 23% free, 12MB/16MB, paused 36.385ms total 94.446ms
04-15 10:32:15.373    3008-3024/jardelcompany.bundoransurfco I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-15 10:32:15.401    3008-3024/jardelcompany.bundoransurfco W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-15 10:32:15.401    3008-3024/jardelcompany.bundoransurfco W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6404200, error=EGL_SUCCESS
04-15 10:32:18.856    3008-3020/jardelcompany.bundoransurfco I/art﹕ Background partial concurrent mark sweep GC freed 7539(430KB) AllocSpace objects, 7(147KB) LOS objects, 18% free, 17MB/21MB, paused 5.183ms total 26.542ms
04-15 10:32:18.879    3008-3023/jardelcompany.bundoransurfco I/System.out﹕ number of `rel`: 1
04-15 10:32:18.879    3008-3023/jardelcompany.bundoransurfco I/MainActivity﹕ first URL : http://www.bundoransurfco.com/wp-content/uploads/2014/11/11110974_10152658294117000_8310999896427501886_n.jpg
04-15 10:32:18.882    3008-3008/jardelcompany.bundoransurfco I/MainActivity﹕ No Background2
04-15 10:32:18.882    3008-3008/jardelcompany.bundoransurfco I/MainActivity﹕ No Background3

0 个答案:

没有答案