我使用 SPenSdk 库创建了一个应用程序。我的应用程序只需打开一个canvasView
,然后我将该画布视图保存为 sd 卡中的图像。现在,当用户从设备中取出 sd 卡时,就会出现问题。当时,当我运行我的应用程序时,它会崩溃,因为它无法找到要加载的图像。那么有什么方法可以保存使用画布视图创建的图像,使其成为应用程序的一部分。所以移除 sd 卡不会影响应用程序。
将图像保存在 SD 卡中的代码
public class CanvasActivity extends Activity
{
public static final String DEFAULT_APP_IMAGEDATA_DIRECTORY = "/mnt/sdcard/SmemoExample";
private File m_Folder = null;
private String m_FileName;
public static final String SAVED_FILE_EXTENSION = "png";
public static final String EXTRA_IMAGE_PATH = "path";
public static final String EXTRA_IMAGE_NAME = "filename";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
m_Folder = new File(DEFAULT_APP_IMAGEDATA_DIRECTORY);
m_FileName = getIntent().getStringExtra(EXTRA_IMAGE_NAME);
loadCanvas(m_FileName);
}
public boolean saveCanvas()
{
byte[] buffer = m_CanvasView.getData();
if (buffer == null)
return false;
if (!(m_Folder.exists()))
m_Folder.mkdirs();
String savePath = null;
if(m_FileName == null)
{
savePath = m_Folder.getPath() + '/' + UtilitiesActivity.getUniqueFilename(m_Folder, "image", SAVED_FILE_EXTENSION);
}
else
{
savePath = m_Folder.getPath() + '/' + m_FileName;
}
if (UtilitiesActivity.writeBytedata(savePath, buffer))
return true;
else
return false;
}
public boolean loadCanvas(String fileName)
{
String loadPath = m_Folder.getPath() + '/' + fileName;
byte[] buffer = UtilitiesActivity.readBytedata(loadPath);
if (buffer == null)
return false;
m_CanvasView.setData(buffer);
return true;
}
private CanvasView.OnHistoryChangeListener historyChangeListener = new CanvasView.OnHistoryChangeListener()
{
@Override
public void onHistoryChanged(boolean bUndoable, boolean bRedoable)
{
m_UndoButton.setEnabled(bUndoable);
m_RedoButton.setEnabled(bRedoable);
}
};
CanvasView.InitializeFinishListener mInitializeFinishListener = new CanvasView.InitializeFinishListener() {
@Override
public void onFinish() {
Bitmap bg = BitmapFactory.decodeResource(getResources(), R.drawable.canvas_bg);
m_CanvasView.setBackgroundImage(bg);
bg.recycle();
}
};
}