BitmapFactory.decodeFile经常在代码中返回null,bug或一些错误?

时间:2014-08-25 09:07:13

标签: android bitmap

我注意到这不是一个新的论点。有很多像这样的讨论。

在我的应用程序中,当我尝试从SD中的文件获取位图时,我经常会得到“NullPointerException”。 通常,并非总是如此!有时它运作良好。所以,我试图了解它是否是一个错误或是否是我的错误。

我在谈论“错误”,因为我读到了这个:Android BitmapFactory.decodeFile intermittently returning null

但是为了确保我把我的代码:

这会在SD中保存Google地图的屏幕截图:

public void CaptureMapScreen() {
    GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
        Bitmap bitmap;

        @Override
        // called when the snapshot is ready
        public void onSnapshotReady(Bitmap snapshot) {
            bitmap = snapshot;
            try {
                // saves the file in the SD
                String path = Environment.getExternalStorageDirectory() + "/MyMapScreen.png";
                FileOutputStream out = new FileOutputStream(path);
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    mGoogleMap.snapshot(callback);
}

这会加载文件,创建位图并修改位图:

private Bitmap imageEditing() {
        // density of the screen
        Resources resources = getResources();
        float scale = resources.getDisplayMetrics().density;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        // bitmap creations
        Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/MyMapScreen.png", options);

        // new bitmap 
        Bitmap highterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + 250, bitmap.getConfig());
 ....

我经常在创建新(更高)位图的行处获得“NullPointerException”。

在这里调用imageEditing:

private class ImageEditing extends AsyncTask<Void, Void, Bitmap> {

    private ProgressDialog mDialog;

    protected void onPreExecute() {
        mDialog = ProgressDialog.show(MainActivity.this, "", "Modifica dell'immagine in corso...", true);
    }

    protected void onPostExecute(Bitmap bitmap) {
        if(mDialog != null) {
            if(mDialog.isShowing()) {
                mDialog.dismiss();
            }
        }
        // saves the new Bitmap in the SD
        String secondPath = Environment.getExternalStorageDirectory() + "/MyMapScreen.png"; // era MyNewMapScreen
        try {
            FileOutputStream out = new FileOutputStream(secondPath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // file to share
        File shareFile = new File(secondPath);

        ShareAsyncTask shareAsyncTask = new ShareAsyncTask();
        shareAsyncTask.execute(shareFile);
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        Bitmap bitmap = imageEditing();
        return bitmap;
    }
}

logcat的:

08-25 11:44:00.357  24646-24646/com.loris.stefano.easyroutes D/OpenGLRenderer﹕ Enabling debug mode 0
08-25 11:44:00.387  24646-24646/com.loris.stefano.easyroutes I/dalvikvm﹕ Failed resolving      Lcom/google/android/gms/location/internal/ParcelableGeofence; interface 4023 'Lglm;'
08-25 11:44:00.387  24646-24646/com.loris.stefano.easyroutes W/dalvikvm﹕ Link of class     'Lcom/google/android/gms/location/internal/ParcelableGeofence;' failed
08-25 11:44:00.387  24646-24646/com.loris.stefano.easyroutes W/dalvikvm﹕ VFY: unable to resolve static field 4203 (CREATOR) in Lcom/google/android/gms/location/internal/ParcelableGeofence;
08-25 11:44:00.387  24646-24646/com.loris.stefano.easyroutes D/dalvikvm﹕ VFY: replacing opcode 0x62 at 0x0016
08-25 11:44:01.967  24646-24648/com.loris.stefano.easyroutes D/dalvikvm﹕ GC_CONCURRENT freed    5281K, 31% free 13696K/19779K, paused 17ms+52ms, total 129ms
08-25 11:44:09.477  24646-24648/com.loris.stefano.easyroutes D/dalvikvm﹕ GC_CONCURRENT freed 5112K, 29% free 14791K/20679K, paused 17ms+15ms, total 118ms
08-25 11:44:11.297  24646-24653/com.loris.stefano.easyroutes I/dalvikvm﹕ Jit: resizing JitTable from 4096 to 8192
08-25 11:44:12.517  24646-25020/com.loris.stefano.easyroutes D/dalvikvm﹕ GC_FOR_ALLOC freed 1814K, 29% free 14874K/20679K, paused 56ms, total 56ms
08-25 11:44:12.657  24646-25022/com.loris.stefano.easyroutes D/dalvikvm﹕ GC_FOR_ALLOC freed 90K, 25% free 18056K/23879K, paused 44ms, total 64ms
08-25 11:44:13.347  24646-25022/com.loris.stefano.easyroutes D/skia﹕ --- decoder->decode returned   false
08-25 11:44:13.467  24646-24646/com.loris.stefano.easyroutes I/Choreographer﹕ Skipped 53 frames!  The application may be doing too much work on its main thread.
08-25 11:44:13.537  24646-25022/com.loris.stefano.easyroutes W/dalvikvm﹕ threadid=25: thread exiting with uncaught exception (group=0x411a0378)
08-25 11:44:14.157  24646-25022/com.loris.stefano.easyroutes E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
 Caused by: java.lang.NullPointerException
        at com.loris.stefano.easyroutes.main.MainActivity.imageEditing(MainActivity.java:1221)
        at com.loris.stefano.easyroutes.main.MainActivity.access$1600(MainActivity.java:99)
        at com.loris.stefano.easyroutes.main.MainActivity$ImageEditing.doInBackground(MainActivity.java:1167)
        at com.loris.stefano.easyroutes.main.MainActivity$ImageEditing.doInBackground(MainActivity.java:1131)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 at java.util.concurrent.FutureTask.run(FutureTask.java:137)

在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) 在java.lang.Thread.run(Thread.java:856) 08-25 11:44:15.547 24646-25022 / com.loris.stefano.easyroutes I / Process:发送信号。 PID:24646 SIG:9

1 个答案:

答案 0 :(得分:1)

// bitmap creations
Bitmap bitmap = BitmapFactory.decodeFile();

// Sleep here
try {
    Thread.sleep(time);    // I set 2000
} catch (InterruptedException e) {
    e.printStackTrace();
}

// new bitmap 
Bitmap highterBitmap = Bitmap.createBitmap();

在错误发生之前添加Sleep()。

这解决了我的问题。