【发布时间】:2017-12-11 11:11:47
【问题描述】:
我正在尝试将 imageView 中的图像存储在位图中,以便可以将其存储在 android 设备的图库中。每次我保存图像时,都不会存储 imageView 的背景。我错过了什么?
这是我的代码:
ImageView imageView = (ImageView) findViewById(R.id.img);
imageView.setBackgroundResource(R.drawable.img1);
BitmapDrawable draw = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = draw.getBitmap();
将图像存储到图库中的代码是:
FileOutputStream outStream = null;
File dir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyAlbum");
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.d("MyAlbum", "failed to create directory");
Toast.makeText(MainActivity.this, "Failed to make directory", Toast.LENGTH_SHORT).show();
}
}
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
try {
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
Toast.makeText(getApplicationContext(), "PICTURE SAVED", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(dir));
sendBroadcast(intent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
【问题讨论】:
-
你有适配器吗?你能分享相关的位置代码
-
将图像存储到存储中的其余代码在哪里??
-
@ankitDemonstrate 你现在可以看到将图像存储到存储中的代码
-
如果一切正常,您是否检查了 WRITE_STORAGE 的运行时权限?
-
您想将 ImageView 的 src 和 ImageView 的背景合并成一个图像并保存?
标签: android android-imageview android-bitmap