J'ai une MainActivity et l'activité contient une vue. Maintenant, je veux enregistrer la vue (ou mieux dit l'image, la vue affiche) sur le stockage interne en cliquant sur le FAB. Le problème est que l'application se bloque toujours avec une exception Pointeur nul. Le bitmap ou en particulier la largeur et la hauteur de la vue semble être nul.Bitmap est toujours null
Mon MainActivtiy
final FractalView fw = new FractalView(this);
fw.invalidate();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Hier wird im finalen Release ein Screenshot gespeichert.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
storeImage(getScreenViewBitmap(fw));
}
});
private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.d(TAG,
"Error creating media file, check storage permissions: ");// e.getMessage());
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 20, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
private Bitmap getScreenViewBitmap(View v) {
v.setDrawingCacheEnabled(true);
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
return b;
//v.setDrawingCacheEnabled(false); // clear drawing cache
}
private File getOutputMediaFile(){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getApplicationContext().getPackageName()
+ "/Files");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
File mediaFile;
String mImageName="MI_" +".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
L'erreur
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.hsd.manguli.fractalsapp, PID: 12149
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at android.graphics.Bitmap.createBitmap(Bitmap.java:619)
at de.hsd.manguli.fractalsapp.MainActivity.getScreenViewBitmap(MainActivity.java:166)
at de.hsd.manguli.fractalsapp.MainActivity.access$000(MainActivity.java:34)
at de.hsd.manguli.fractalsapp.MainActivity$1.onClick(MainActivity.java:69)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
J'espère que nous pourrons résoudre le problème. Merci beaucoup :)
S'il vous plaît mettre la stacktrace d'exception –
@DanyPoplawec j'ai ajouté l'exception :) – Tommy