我有一个使用网址加载的imageview。 我想在视图中下载图像并将其存储在移动设备的内部/外部驱动器中。但我尝试了Stackoverflow上所有可能的方法,即使在移动设备上给出的读取和写入外部存储器中的预先设置,也无法访问驱动器。似乎没什么用,我不知道为什么?
这是我的代码,你能否告诉我哪里出错了?
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCardDirectory = Environment.getDataDirectory();
File image = new File(sdCardDirectory, "test.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
清单权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 0 :(得分:0)
File sdCardDirectory = Environment.getDataDirectory();
那不是SD卡目录。
Environment.getDataDirectory().getAbsolutePath() == /data
你没有写权限。更好地使用
File sdCardDirectory = Environment.getExternalStorageDirectory();
如果您的权限得到了解,那将会有效。
但它不会保存到SD卡中!