After my tablet update to 4.4.2 my app crashes.
My log says at com.example.androidhive.EditProductActivity$6.onClick(EditProductActivity.java:344
)
and the line is
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
This is a part off my code (the bitmap code)
@Override
public void onClick(View v) {
btn_shoot.setVisibility(View.INVISIBLE);
View view = findViewById(R.id.relativelayout);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setBackgroundDrawable(bitmapDrawable);
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
//we check if external storage is available, otherwise display an error message to the user
File sdCard = Environment.getExternalStorageDirectory();
// .getExternalStorageDirectory() + "/Indflyt/ "+ inputNr.getText().toString() + ".pdf";
File directory = new File (sdCard.getAbsolutePath() + "/Indflyt");
directory.mkdirs();
String filename = inputNr.getText().toString() + "-"+ txtetDate.getText().toString() +".jpg";
File yourFile = new File(directory, filename);
while (yourFile.exists()) {
i++;
filename = "screenshot" + i + ".jpg";
yourFile = new File(directory, filename);
}
if (!yourFile.exists()) {
if (directory.canWrite())
{
try {
FileOutputStream out = new FileOutputStream(yourFile, true);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Toast.makeText(EditProductActivity.this, "Billede gemt i /sdcard/Indflyt/" + inputNr.getText().toString() + "-" + txtetDate.getText().toString() + ".jpg", Toast.LENGTH_SHORT).show();
i++;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
else
{
Toast.makeText(EditProductActivity.this, "SD Card not available!", Toast.LENGTH_SHORT).show();
}
}
});
and my log
11-26 11:43:33.413: E/AndroidRuntime(24275): FATAL EXCEPTION: main
11-26 11:43:33.413: E/AndroidRuntime(24275): Process: com.example.androidhive, PID: 24275
11-26 11:43:33.413: E/AndroidRuntime(24275): java.lang.NullPointerException
11-26 11:43:33.413: E/AndroidRuntime(24275): at com.example.androidhive.EditProductActivity$6.onClick(EditProductActivity.java:344)
11-26 11:43:33.413: E/AndroidRuntime(24275): at android.view.View.performClick(View.java:4630)
11-26 11:43:33.413: E/AndroidRuntime(24275): at android.view.View$PerformClick.run(View.java:19340)
11-26 11:43:33.413: E/AndroidRuntime(24275): at android.os.Handler.handleCallback(Handler.java:733)
11-26 11:43:33.413: E/AndroidRuntime(24275): at andrid.os.Handler.dispatchMessage(Handler.java:95)
11-26 11:43:33.413: E/AndroidRuntime(24275): at android.os.Looper.loop(Looper.java:157)
11-26 11:43:33.413: E/AndroidRuntime(24275): at android.app.ActivityThread.main(ActivityThread.java:5377)
11-26 11:43:33.413: E/AndroidRuntime(24275): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 11:43:33.413: E/AndroidRuntime(24275): at java.lang.reflect.Method.invoke(Method.java:515)
11-26 11:43:33.413: E/AndroidRuntime(24275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-26 11:43:33.413: E/AndroidRuntime(24275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-26 11:43:33.413: E/AndroidRuntime(24275): at dalvik.system.NativeStart.main(Native Method)
Any help?
Update
View view = findViewById(R.id.relativelayout);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Drawable bitmapDrawable = null;
iv.setBackground(bitmapDrawable);