【问题标题】:bitmap loading so long in android app位图在android应用程序中加载这么长时间
【发布时间】:2013-07-18 12:12:34
【问题描述】:

大家好!

我有一个小问题但是一个问题!

我在 2 个活动之间传输最多 6 张照片。但是 2 之间的加载非常长(6-10 秒)。

我的代码:

String[] toShare = getIntent().getStringArrayExtra("toShare");

    for (int i = 0; i < toShare.length; i++) {
        if(toShare[i] != null){
            LesPlus.get(i).setImageBitmap(genereThumb(toShare[i]));
        }else{
            LesPlus.get(i).setImageDrawable(getResources().getDrawable(R.drawable.btnadd));
        }
    }

基因拇指:

    File file = new File(path);
    FileInputStream fs=null;
    Bitmap bm = null;

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int largeurEcran = dm.widthPixels;
    int LargeurCol = (int) ((largeurEcran / 3) - 15);

    BitmapFactory.Options bfOptions=new BitmapFactory.Options();
    bfOptions.inDither=false;
    bfOptions.inPurgeable=true;
    bfOptions.inInputShareable=true;
    bfOptions.inTempStorage=new byte[32 * 1024]; 

    try {
        fs = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        System.out.println(e);
    }

    try {
        if(fs!=null) bm=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
    } catch (IOException e) {
        e.printStackTrace();
    } finally{ 
        if(fs!=null) {
            try {
                fs.close();
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }

    return Bitmap.createScaledBitmap(bm, LargeurCol, LargeurCol, true);
}

如果有帮助的话,我会开发 Galaxy S2 :D。谢谢你的回答

【问题讨论】:

  • 要管理位图,建议使用硬缓存。看看github.com/nostra13/Android-Universal-Image-Loader 这是迄今为止我所知道的最好的库来管理下载,redim;缓存和显示图像
  • 谢谢,但我没有找到任何关于缩略图的信息 :( 我想使用默认的 android 库
  • 我试过了,但它什么也没显示,而且 .writeDebugLogs() 不起作用!它需要一个演员:(
  • 你说的是通用图片加载器吗?
  • 还有一点,你在位图上的工作不应该在主线程上;)

标签: android bitmap


【解决方案1】:
【解决方案2】:

非常感谢!我当时读过,如果我明白了,

如果我用这个:

public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
/** The system calls this to perform work in a worker thread and
  * delivers it the parameters given to AsyncTask.execute() */
protected Bitmap doInBackground(String... urls) {
    return loadImageFromNetwork(urls[0]);
}

/** The system calls this to perform work in the UI thread and delivers
  * the result from doInBackground() */
protected void onPostExecute(Bitmap result) {
    mImageView.setImageBitmap(result);
}

}

我的位图加载速度会更快吗?如果它真的那么强大

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多