2011-09-20 43 views
3

您好我創造了這個方法做了如下因素:Android的讀 - 寫臨時文件

- 它需要一個位圖

- 創建臨時文件

-compress位圖,並把它放在臨時文件

-open臨時文件

- 讀取字節>>>問題是在我的代碼

- 保存在sqlite的分貝這些字節從sqlite的分貝

-retrieve字節

-set這些字節作爲位圖圖像再次

我知道,我知道完全無用的,但是這是唯一的辦法,我發現做我要實現它是什麼:

-compress位圖

- 保存它SQLite數據庫

所以這裏是代碼。程序只是在in.read(bb)崩潰:

private void updateBitmapData() { 
    File file = null; 

    FileOutputStream stream = null; 

    try { 
     file = File.createTempFile("image", null, getBaseContext().getFilesDir()); 
     stream = new FileOutputStream(file); 
     System.out.println(thumbnail.getRowBytes()); 
     thumbnail.compress(Bitmap.CompressFormat.JPEG, 50, stream); 

     stream.close(); 

     FileInputStream in=new FileInputStream(file); 

     byte[] bb = null; 
     in.read(bb); //crash 
     in.close(); 
     System.out.println("despues compression"+bb.length); 

     DBAdapter db=new DBAdapter(getApplicationContext()); 
     db.insertData(bb); 
     Cursor c=db.getLastImage(); 
     if(c.moveToFirst()){ 
      bb=c.getBlob(0); 
     } 
     c.close(); 
     db.close(); 
     im.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length)); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Log.e("NuevaIncidencia", e.getLocalizedMessage()); 
     im.setImageBitmap(thumbnail); 
    }catch (IOException e1) { 
     Log.e("NuevaIncidencia", e1.getLocalizedMessage()); 
     e1.printStackTrace(); 
     im.setImageBitmap(thumbnail); 
    } catch (Exception e2){ 
     e2.printStackTrace(); 
     //Log.e("NuevaIncidencia", e2.getLocalizedMessage()); 
    } 

} 

我怎麼能解決這個問題?謝謝

+0

爲什麼你不嘗試保存/ SD卡上只檢索圖片的路徑。它很容易使用。 –

回答

1

我想你得到一個NullPointerException,並且這似乎是正常的,因爲當你調用read時你的緩衝區爲空。

您必須實例化預期大小的緩衝區。

嘗試初始化您的字節數組是這樣的:

byte[] bb = new byte[file.length()];

希望它可以幫助

Jokahero

+0

是的,這是問題之一 – vallllll