Android BitmapFactory.decodeFile经常返回null、bug或代码中的某些错误?
我注意到这不是一个新的论点。有很多这样的讨论 在我的应用程序中,当我试图从SD中的文件获取位图时,经常会得到“NullPointerException”。 经常,不总是!有时效果很好。所以,我想知道这是一个bug还是一个我的错误 我之所以说“虫子”,是因为我读到: 但为了确保我的代码: 这将在SD中保存Google地图的屏幕截图:Android BitmapFactory.decodeFile经常返回null、bug或代码中的某些错误?,android,bitmap,Android,Bitmap,我注意到这不是一个新的论点。有很多这样的讨论 在我的应用程序中,当我试图从SD中的文件获取位图时,经常会得到“NullPointerException”。 经常,不总是!有时效果很好。所以,我想知道这是一个bug还是一个我的错误 我之所以说“虫子”,是因为我读到: 但为了确保我的代码: 这将在SD中保存Google地图的屏幕截图: public void CaptureMapScreen() { GoogleMap.SnapshotReadyCallback callback = new
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”
此处称为图像编辑:
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;
}
}
在android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
运行(Thread.java:856)
08-25 11:44:15.547 24646-25022/com.loris.stefano.easyroutes I/Process﹕ 发送信号。PID:24646信号:9
// 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()
这解决了我的问题。您从哪里调用imageEditing()您可以发布logcat吗?我编辑了主题:)可能返回null,但您正在使用
bitmap
,方法是在该行之后调用getWidth()
。您需要处理无法解码文件的情况。
// 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();