2016-09-07 20 views
1

IM而在SQLite數據庫保存圖像面對這個錯誤,圖像大小隻有240KB我已經通過了一些解決方案了,但沒能解決這個問題,我的代碼爲DB的OutOfMemoryError在SQliteDatabase保存圖像

public class MainActivity extends AppCompatActivity { 

SQLiteDatabase db; 
ImageView img,img_get; 
Button btn_save,btn_get; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    img=(ImageView)findViewById(R.id.img_save); 
    img_get=(ImageView)findViewById(R.id.img_get); 
    btn_get=(Button)findViewById(R.id.btn_get); 
    btn_save=(Button)findViewById(R.id.btn_save); 
    db= openOrCreateDatabase("Mydb", MODE_PRIVATE, null); 
    db.execSQL("create table if not exists hello(name varchar, a blob)"); 


    btn_save.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) 
     { 
     saveimage(v); 

     } 
    }); 

    btn_get.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) 
     { 
      getimage(v); 
     } 
    }); 
} 
public void saveimage(View view) 
{ 
    byte[] image=new byte[R.drawable.red]; 
    ContentValues value=new ContentValues(); 
    value.put("name","hatib abrar"); 
    value.put("a",image); 
    db.insert("hello",null,value); 
    Toast.makeText(getApplicationContext(),"Saved",Toast.LENGTH_SHORT).show(); 
} 
public void getimage(View view) 
{ 
    Cursor c=db.rawQuery("select * from hello",null); 
    if (c.moveToNext()) 
    { 
     String name=c.getString(0); 
     byte[] image=c.getBlob(1); 
     Bitmap bm= BitmapFactory.decodeByteArray(image,0,image.length); 
     img_get.setImageBitmap(bm); 
     Toast.makeText(getApplicationContext(),"Error getting image",Toast.LENGTH_SHORT).show(); 
    } 

} 
保存和獲取圖像

logcat的是顯示這個錯誤 投擲

OutOfMemoryError "Failed to allocate a 2130837597 byte allocation with 16777216 free bytes and 151MB until OOM" 09-07 09:49:59.651 21823-21823/com.example.e6530.imagesaving D/AndroidRuntime: Shutting down VM 09-07 09:49:59.651 21823-21823/com.example.e6530.imagesaving E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.e6530.imagesaving, PID: 21823 java.lang.OutOfMemoryError: Failed to allocate a 2130837597 byte allocation with 16777216 free bytes and 151MB until OOM at com.example.e6530.imagesaving.MainActivity.saveimage(MainActivity.java:51) at com.example.e6530.imagesaving.MainActivity$1.onClick(MainActivity.java:36) at android.view.View.performClick(View.java:4793) at android.view.View$PerformClick.run(View.java:19971) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5669) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

+0

你爲什麼不把圖像保存在SD卡或其他東西?在數據庫中存儲圖像是不好的做法,相反,您可以將路徑存儲在分貝中,實際圖像可以存儲在SD卡中。 – himanshu1496

+0

生成的位圖大小不取決於圖像的大小。它取決於該圖像的分辨率 – Prashant

+0

大圖像???? –

回答

2

您的問題是在這條線:

byte[] image=new byte[R.drawable.red]; 

經常R.drawable是一個大整數。例如10億,所以你嘗試創建數十億字節的數組。這是很多的記憶。請不要將資源ID作爲數組大小傳遞。

更新

請參閱this答案,this的頁面來處理如何從資源獲取位圖,並將其轉換爲字節數組。也許你需要寫類似:

Resource r = ...; 
Bitmap bitmap = Bitmap.decodeResource(r, R.drawable.red); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
byte[] bitmapdata = stream.toByteArray(); 
+0

是的,我認爲這個問題是與這如何分配圖像的字節? m測試這個模塊,因爲我必須從列表中取出youtube視頻的縮略圖並將其存儲到sqlite數據庫 –

+0

@hatibabrar更新了我的答案 –

+0

讓我檢查這個bro –

2

此:

byte[] image=new byte[R.drawable.red]; 

沒有做什麼,你認爲它。 R.drawable.red是生成的整數ID,可能相當高。在你的情況下,像2130837597,這是你嘗試分配的字節量。

你必須先加載你的圖像,然後得到它的字節並將它們加載到你的數據庫中。快速搜索出現this for example

+0

讓我檢查兄弟這是的,你是對的它顯示我的大數我無法得到它 –

+0

感謝兄弟爲了得到我的問題工作就像一個魅力:) –