Я пытаюсь сохранить RelativeLayout
как bitmap
и вставить его в галерею, код ниже выдает это исключение:
java.lang.NullPointerException
at java.io.File.<init>(File.java:282)
at maa.Fragements.ColorPaletteFragment.galleryAddPic(ColorPaletteFragment.java:344)
, поскольку параметр imagePath
из galleryAddPic
метод пуст или ноль
вставить изображение в галерею:
private void galleryAddPic(String imagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
getActivity().sendBroadcast(mediaScanIntent);
}
сохранить изображение:
@SuppressLint("StaticFieldLeak")
public class saveViewAsBitmap extends AsyncTask<RelativeLayout, String, String> {
@SuppressLint("SetTextI18n")
@Override
protected void onPreExecute() {
super.onPreExecute();
txt.setText("Saving image to gallery ...");
}
@Override
protected String doInBackground(RelativeLayout... relatives) {
String savedImagePath = null;
String imageFileName = "inColor" + System.currentTimeMillis() + ".png";
Bitmap image = getBitmapFromView(relatives[0]);
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/inColor Folder");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
dialogLoading.dismiss();
}
return savedImagePath;
}
@Override
protected void onPostExecute(String path) {
super.onPostExecute(path);
galleryAddPic(path);
}
}
Может ли кто-нибудь помочь мне решить эту проблему?