به نام خدا
سلام. برای ذخیره تصویر موجود در ImageView داخل حافظه خارجی دیوایس کاربر مراحل زیر رو پیش می ریم:
1- افزودن دو متد زیر برای ذخیره تصویر Bitmap گرفته شده:
img نام ImageView مورد نظر ما می باشد.
۳-افزودن اجازه دسترسی به حافظه خارجی:


img نام ImageView مورد نظر ما می باشد.
private void save() {2- صدا زدن متد save() در جایی که میخوایم تصویرمون ذخیره بشه.
FileOutputStream out = null;
Bitmap bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap();
try {
out = new FileOutputStream(getFilename());
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
Snackbar.make(view, "تصویر در پوشه esfanduneذخیره شد", Snackbar.LENGTH_SHORT)
.show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Snackbar.make(view, "خطایی در ذخیره سازی رخ داد", Snackbar.LENGTH_SHORT).show();
}
}
private String getFilename() {
File file = new File(Environment.getExternalStorageDirectory()
.getPath(), "esfandune");
if (!file.exists()) {
file.mkdirs();
}
String uriSting = (file.getAbsolutePath() + "/"
+ "nami_ke_mikhayd_ax_zakhire_beshe" + ".jpg");
return uriSting;
}
۳-افزودن اجازه دسترسی به حافظه خارجی:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


- ۲ نظر
- ۲۳ دی ۹۴ ، ۱۰:۳۶