我在绘制,然后保存图像,然后再次正确地将其加载到“画布”中吗?

问题描述

所以我要在画布上绘制此应用程序,然后将绘制的内容作为位图保存在应用程序特定的存储中(可以说是敏感内容),然后我应该能够再次加载位图并进行修改并再次保存。 在下面的代码中,我将保存从绘图中获取的位图。目录是文件类型

public  String saveImage(String filename,Bitmap image){
    File file=new File(directory,filename+"jpg");

    try {
        FileOutputStream out = new FileOutputStream(file);
        image.compress(Bitmap.CompressFormat.JPEG,90,out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        System.out.println("FileNotFoundException");
        e.printstacktrace();
    } catch (IOException e) {
        System.out.println("IOException");
        e.printstacktrace();
    }
    return file.getAbsolutePath();
}

这是我从内部存储加载图像的方式:

public  Bitmap loadImage(String filename) throws FileNotFoundException {
    File f = new File(directory,filename + "jpg");
    Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
    return convertToMutable(context,b);
}

我不得不添加这种我不完全理解的方法来将位图转换为可变的:

public static Bitmap convertToMutable(final Context context,final Bitmap imgIn) {
    final int width = imgIn.getWidth(),height = imgIn.getHeight();
    final Bitmap.Config type = imgIn.getConfig();
    File outputFile = null;
    final File outputDir = context.getCacheDir();
    try {
        outputFile = File.createTempFile(Long.toString(System.currentTimeMillis()),null,outputDir);
        outputFile.deleteOnExit();
        final RandomAccessFile randomAccessFile = new RandomAccessFile(outputFile,"rw");
        final FileChannel channel = randomAccessFile.getChannel();
        final MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE,imgIn.getRowBytes() * height);
        imgIn.copyPixelsToBuffer(map);
        imgIn.recycle();
        final Bitmap result = Bitmap.createBitmap(width,height,type);
        map.position(0);
        result.copyPixelsFromBuffer(map);
        channel.close();
        randomAccessFile.close();
        outputFile.delete();
        return result;
    } catch (final Exception e) {
    } finally {
        if (outputFile != null)
            outputFile.delete();
    }
    return null;
}

我正在这样设置画布:

public void setCanvasBitmap(Bitmap b){
    drawCanvas=new Canvas(b);
}

希望它不会太长。问题在于它不会在画布上显示我之前保存的位图。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)