从文件路径获取位图

Rad*_*jen 1 android android-bitmap android-thread

所以问题是我想从绝对路径获取位图,所以我将这些路径传递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());

}
Run Code Online (Sandbox Code Playgroud)

这是我的 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();
}
Run Code Online (Sandbox Code Playgroud)

}

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

bwt*_*bwt 7

你没有递增imageCounter,所以它实际上是一个无限循环。

增强的 for 循环更不容易出错:

for (String uri : imageUris) {
    ...
    Bitmap bitmap = BitmapFactory.decodeFile(uri);
    ...
Run Code Online (Sandbox Code Playgroud)