我按照在线教程制作了一个简单的绘图应用程序。一切正常,直到图形保存到内部存储的部分。出于某种原因,即使分配了明确的名称,图像名称也始终为空。我在运行Android Nougat的Axon 7 Mini上运行该应用程序。这是我无法工作的代码。我在Manifest中得到了适当的许可。如果将图像保存为JPEG与PNG,这是否重要?
Picture can't be saved into phone storage
else if(view.getId()==R.id.save_btn){
//save drawing
AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
saveDialog.setTitle("Save drawing");
saveDialog.setMessage("Save drawing to device Gallery?");
saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
//save drawing
drawView.setDrawingCacheEnabled(true);
//attempt to save
String imgSaved = null;
OutputStream fOut = null;
File mediaDirectory = new File(Environment.getExternalStorageDirectory().getPath()
+ "DCIM/Camera/");
mediaDirectory.mkdirs();
File f = new File(mediaDirectory, UUID.randomUUID().toString() + ".png");
try {
fOut = new FileOutputStream(f);
drawView.getDrawingCache().compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
imgSaved = "success";
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse
("file://" + Environment.getExternalStorageDirectory())));
}
catch (Exception e) {
e.printStackTrace();
}
//feedback
if(imgSaved!=null){
Toast savedToast = Toast.makeText(getApplicationContext(),
"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
savedToast.show();
}
else{
Toast unsavedToast = Toast.makeText(getApplicationContext(),
"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}
drawView.destroyDrawingCache();
}
});
saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
saveDialog.show();
}