如何将图像保存在我的应用程序数据库中,而不是sd-card

时间:2012-05-18 11:13:16

标签: android database android-canvas

我使用SPenSdk库创建了一个应用程序。我的应用只需打开一个canvasView,我就将该画布视图保存为SD卡中的图像。现在,当用户从设备中删除SD卡时会出现问题。那时我运行我的应用程序它崩溃,因为它无法找到加载图像。那么有什么方法可以保存我使用画布视图创建的图像,这样它将成为我应用程序的一部分。所以删除SD卡不会影响应用程序。

在SdCard中保存图像的代码

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();
    }
};
}

1 个答案:

答案 0 :(得分:0)

问题不是数据库或非数据库问题是你的图像在哪里。设备上的空间有限,但如果您不想在SD卡上存储图像,则应将它们存储在设备的板载内存中。将它们填充到数据库blob中对你没有帮助(实际上,它只是让事情变得更复杂)。

以下是您的操作方法: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal