简体   繁体   English

如何将图像保存在我的应用程序数据库而不是 SD 卡中

[英]How to save the images in my application database rather then sd-card

I have created an app using SPenSdk libraries.我使用 SPenSdk 库创建了一个应用程序。 My app simply open an canvasView and I saved that canvas view as an image in the sd card.我的应用程序只需打开一个canvasView然后将该画布视图作为图像保存在 SD 卡中。 Now problems arise when the user removes it sd card from the devices.现在,当用户从设备中取出 SD 卡时会出现问题。 At that time when I run my app it crashes because it is unable to find images for loading.那时当我运行我的应用程序时它崩溃了,因为它无法找到要加载的图像。 So is there any way in which I can save my images created using canvas view in such a way that it will be my part of the app.那么有什么方法可以保存使用画布视图创建的图像,使其成为我的应用程序的一部分。 so the removing of sd card doesn't affect the app.所以删除sd卡不会影响应用程序。

Code For Saving Images In SdCard在 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();
    }
};
}

The issue isn't database or non database the issue is WHERE are your images.问题不在于数据库或非数据库问题在于您的图像在哪里。 There's limited space ON the device, but if you don't want to store images on the sdcard you should just store them on the device's onboard memory.设备上的空间有限,但如果您不想将图像存储在 SD 卡上,您应该将它们存储在设备的板载内存中。 Stuffing them into database blobs doesn't help you here (actually, it just makes things more complicated).将它们填充到数据库 blob 中对您没有帮助(实际上,它只会让事情变得更复杂)。

Here's how you do it: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal这是你的方法: http : //developer.android.com/guide/topics/data/data-storage.html#filesInternal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM