本文介绍了位图保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实施撤销,并在Android的应用程序fingerpaint重做功能。我曾尝试使用下面的code来实现。

错误

  20 03-26:42:12.020:W / System.err的(28056):显示java.lang.NullPointerException
03-26 20:42:12.020:W / System.err的(28056):在baked.soft.FirstActivity.toJPEGFile(FirstActivity.java:341)
03-26 20:42:12.020:W / System.err的(28056):在baked.soft.FirstActivity.onOptionsItemSelected(FirstActivity.java:314)

FirstActivity

 公共布尔onOptionsItemSelected(菜单项项){
     开关(item.getItemId()){
     案例R.id.tools:
        ll.setVisibility(LinearLayout.VISIBLE);
        返回true;
     案例R.id.import_pics:
         getPhotos();
         返回true;
     案例R.id.save:
        toJPEGFile();
     返回true;
     案例R.id.trash:
         垃圾();         返回true;
     }
    返回false;}私人无效toJPEGFile(){
    // TODO自动生成方法存根
    。字符串根= Environment.getExternalStorageDirectory()的toString();
    文件MYDIR =新的文件(根+/ saved_images);
    myDir.mkdirs();
    随机数发生器=新的随机();
    INT N = 10000;
    N = generator.nextInt(N);
    字符串FNAME =图像 - + N +JPG;
    档案文件=新的文件(MYDIR,FNAME);
    如果(file.exists())file.delete();
    尝试{
           FileOutputStream中出=新的FileOutputStream(文件);
           finalBitmap.com preSS(Bitmap.Com pressFormat.JPEG,90出); //错误341线
           了out.flush();
           out.close();    }赶上(例外五){
           e.printStackTrace();
    }
}

我不知道哪里错了。请帮帮我。
在此先感谢!


解决方案

  finalBitmap.com preSS(Bitmap.Com pressFormat.JPEG,90出);

正如你所指出的错误行。

finalBitmap 尚未初始化。

阿卡,它不是一个位图,它是一个空值。确保装入你的位图到这个变量第一位。

  finalBitmap = .... //分配finalBitmap。
finalBitmap.com preSS(Bitmap.Com pressFormat.JPEG,90出); //然后保存到文件

编辑:

从您的评论阅读,一种方式做到这一点是做到以下几点,在的onDraw 方法:

  canvas.drawBitmap(位图,0,0,油漆);
  finalBitmap =位图;

但同样,我已经不知道你在哪里得到的位图。这又是一个全局变量?也许你可以只改变 finalBitmap.com preSS ... bitmap.com preSS ...

I have tried to implement undo and redo feature in android fingerpaint app. I have tried to implement using the below code.

Error

03-26 20:42:12.020: W/System.err(28056): java.lang.NullPointerException
03-26 20:42:12.020: W/System.err(28056):    at baked.soft.FirstActivity.toJPEGFile(FirstActivity.java:341)
03-26 20:42:12.020: W/System.err(28056):    at baked.soft.FirstActivity.onOptionsItemSelected(FirstActivity.java:314)

FirstActivity

public boolean onOptionsItemSelected(MenuItem item){
     switch (item.getItemId()) {
     case R.id.tools:
        ll.setVisibility(LinearLayout.VISIBLE);
        return true;
     case R.id.import_pics:
         getPhotos();
         return true;
     case R.id.save:
        toJPEGFile();
     return true;
     case R.id.trash:
         trash();

         return true;
     }
    return false;

}

private void toJPEGFile() {
    // TODO Auto-generated method stub
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete ();
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); // ERROR 341 LINE
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}

I don't know where is wrong. please help me.Thanks in advance!

解决方案
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

as you point out is the error line.

finalBitmap has not been initialised.

Aka, it is not a bitmap, it is a null value. Make sure you load your bitmap into this variable first.

finalBitmap = .... // assign finalBitmap.
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);  // Then save to file

Edit:

reading from your comment, one way to do it would be to do the following, in onDraw method:

  canvas.drawBitmap(bitmap, 0, 0, paint);
  finalBitmap = bitmap;

But again, ive no idea where you are getting bitmap from. Is this another global variable? Maybe you could just change finalBitmap.compress... to bitmap.compress...

这篇关于位图保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:02