我正在构建一个应用程序以使用相机拍照,但我不知道我可以将我的应用程序的照片保存在avd android(我的AVD:Nexus 5X API 26 Android 8.0)的sd卡上。 我拍了照片,并在应用程序中显示了照片,但在为应用程序创建的文件夹中(在SDCard中)找不到该照片。谢谢。
答案 0 :(得分:1)
首先在清单中添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
,因此您需要获取Bitmap。您可以尝试从ImageView获取它,例如:
BitmapDrawable drawable = (BitmapDrawable) mImageView1.getDrawable();
Bitmap bitmap = drawable.getBitmap();
您必须从SD卡进入目录
File sdCardDirectory = Environment.getExternalStorageDirectory();
// Next, create your specific file for image storage:
File image = new File(sdCardDirectory, "test.png");
//After that, you just have to write the Bitmap :
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();
}