从文件路径获取位图

时间:2018-06-15 08:36:24

标签: android android-bitmap android-thread

所以问题是我想从绝对路径获取Bitmap,所以我将这些路径作为ArrayList<Strings>传递给我的演示者,我有下一段代码:

private void decodeImageUri(final ArrayList<String> imageUris) {

    while(imageCounter < imageUris.size()) {
        DecodeBitmapsThreadPool.post(new Runnable() {
            @Override
            public void run() {

                Bitmap bitmap = BitmapFactory.decodeFile(imageUris.get(imageCounter));

                mImagesBase64Array.add(bitmapToBase64(bitmap));
            }
        });
    }
    DecodeBitmapsThreadPool.finish();
    Log.d("SIZE OF BASE64", " ---------- " + mImagesBase64Array.size());

}

这是我的ThreadPool类:

public class DecodeBitmapsThreadPool {

private static DecodeBitmapsThreadPool mInstance;
private ThreadPoolExecutor mThreadPoolExec;
private static int MAX_POOL_SIZE;
private static final int KEEP_ALIVE = 10;
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();

public static synchronized void post(Runnable runnable) {
    if (mInstance == null) {
        mInstance = new DecodeBitmapsThreadPool();
    }
    mInstance.mThreadPoolExec.execute(runnable);
}

private DecodeBitmapsThreadPool() {
    int coreNum = Runtime.getRuntime().availableProcessors();
    MAX_POOL_SIZE = coreNum * 2;
    mThreadPoolExec = new ThreadPoolExecutor(
            coreNum,
            MAX_POOL_SIZE,
            KEEP_ALIVE,
            TimeUnit.SECONDS,
            workQueue);
}

public static void finish() {
    mInstance.mThreadPoolExec.shutdown();
}

}

因此,当我启动ThreadPool时,它看起来像某种无限循环(根据Logcat),然后我得到OutOfMemoryException。我不知道我做错了什么,因为我无法调试它。我只是想在后台线程中解码位图并创建那些位图的base64表示,所以我可以将它们上传到服务器。附:任何想法如何用RxJava2实现?提前致谢!

3 个答案:

答案 0 :(得分:2)

你没有递增WHERE UpdateCode IN (‘ABC’, 'ZYX') or InsertCode IN (‘ABC’, 'ZYX') ,所以它实际上是一个无限循环。

增强的for循环不易出错:

SELECT 
    ds1.A
    ds2.B
FROM (
    Select
        1 AS ID,
        Max(isnull(UpdateDate, InsertDate)) as A        
    From yourTable
    Where UpdateCode = 'ABC' or InsertCode = 'ABC'
) ds1
INNER JOIN (
    Select
        1 AS ID,
        Max(isnull(UpdateDate, InsertDate)) as B
    From yourTable
    Where UpdateCode = 'ZYX' or InsertCode = 'ZYX'
) ds2
ON ds1.ID = ds2.ID

答案 1 :(得分:1)

while循环中

imageCounter 值未更改,因此此条件( imageCounter&lt; imageUris.size())始终为true且循环运行无限次

 while(imageCounter < imageUris.size()) {
        DecodeBitmapsThreadPool.post(new Runnable() {
            @Override
            public void run() {

                Bitmap bitmap = BitmapFactory.decodeFile(imageUris.get(imageCounter));

                mImagesBase64Array.add(bitmapToBase64(bitmap));
            }
        });
    }

答案 2 :(得分:0)

问题是您不会增加while循环,因此while循环的条件将永远不会得到满足。您应该在while循环内的代码末尾增加它,如下所示:

while(imageCounter < imageUris.size()) {
    DecodeBitmapsThreadPool.post(new Runnable() {
        @Override
        public void run() {

            Bitmap bitmap = BitmapFactory.decodeFile(imageUris.get(imageCounter));

            mImagesBase64Array.add(bitmapToBase64(bitmap));
        }
    });

    imageCounter++;
}

尽管如bwt的回答所述,增强的for循环通常是处理此类任务的最佳方法。