Je n'ai pas pu enregistrer l'image dans le répertoire externe de mon application Android. Je m'entraîne et cherche le même problème mais rien ne peut m'aider. Tout ce que je veux faire est d'afficher une boîte de dialogue avec cette image et donner l'utilisateur une option pour enregistrer et si vue enregistré. Mais il attrape toujours une exception de pointeur null à out.flush(); dans la guimauve et supérieure. fonctionne très bien dans lollipop. autorisations ajoutées:Impossible d'enregistrer l'image dans le répertoire externe
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
code
me semble bien à. Toute aide serait très appréciée.
private void display_image(String url, String title) {
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_display_images);
dialog.setTitle(title);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.show();
dialog.getWindow().setAttributes(layoutParams);
ImageView iv = (ImageView)dialog.findViewById(R.id.image_here);
final InputStream in;
Bitmap img=null;
final Bitmap imgcpy;
try {
in = getAssets().open(url);
img = BitmapFactory.decodeStream(in);
iv.setImageBitmap(img);
} catch (IOException e) {
e.printStackTrace();
}
imgcpy = img;
final FloatingActionButton save = (FloatingActionButton)dialog.findViewById(R.id.fab_save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String filePath = Environment.getExternalStorageDirectory().toString();
File dir = new File(filePath + "/app_images");
dir.mkdirs();
Random generate = new Random();
int n = 10000;
n = generate.nextInt(n);
String fName = "Image-" + n + ".jpg";
final File file = new File(dir, fName);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imgcpy.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
new AlertDialog.Builder(MainActivity.this)
.setTitle("Image Saved Successfully")
.setIcon(R.drawable.ic_save)
.setMessage("Image saved at: " + file.getAbsolutePath())
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("Open", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Opening...", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + file.getAbsolutePath()), "image/*");
startActivity(intent);
}
})
.create().show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Could not save image", Toast.LENGTH_SHORT).show();
} catch (NullPointerException e) {
Toast.makeText(MainActivity.this, "Could not save image", Toast.LENGTH_SHORT).show();
}
}
});
}