首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android应用程序中的位图加载时间过长

android应用程序中的位图加载时间过长
EN

Stack Overflow用户
提问于 2013-07-18 20:12:34
回答 2查看 117关注 0票数 1

大家好!

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

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

我的代码:

代码语言:javascript
复制
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));
        }
    }

genereThumb:

代码语言:javascript
复制
    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上开发,如果它能帮上忙的话:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-19 15:49:53

指向AsyncTask文档的链接:

http://developer.android.com/reference/android/os/AsyncTask.html

下面是一些教程:

http://android-developers.blogspot.fr/2009/05/painless-threading.html

指向线程文档的链接:

http://developer.android.com/guide/components/processes-and-threads.html

http://developer.android.com/guide/faq/commontasks.html#threading

票数 0
EN

Stack Overflow用户

发布于 2013-07-19 16:04:52

非常感谢!我已经读过了,如果我懂了,

如果我使用这个:

代码语言:javascript
复制
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");

}

代码语言:javascript
复制
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);
}

}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17723124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档