2015-04-30 28 views
0

我想開發一個Android應用程序,其中有一個選項可以從手機存儲器上傳圖像,然後它將顯示在列表中查看用戶給出的一些標題。
我已經創建了它的佈局,但我不知道如何實現這個概念。

我用一個的EditText和一個按鈕,在LinearLayout中上傳文件,並使用ListView的另一個動作來顯示圖像我怎麼上傳圖像從手機內存到Android的SQLite數據庫

+0

發表你試過的東西?否則你會自動downvote .. –

+0

我想知道,如果你甚至在發佈此之前尋找答案... https://stackoverflow.com/questions/9357668/how-to-store-image-in-sqlite-database – nasch

+0

我使用了一個EditText和一個按鈕來在LinearLayout中上傳文件,並在另一個活動中使用ListView來顯示圖像。 @ranjith –

回答

1

首先,你必須獲得從手機內存中的圖像,比如`

Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT); 
photoPickerIntent.setType("image/*"); 
startActivityForResult(photoPickerIntent, 1); 
    Bitmap mBitmap = null; 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) 
    { 
     Uri chosenImageUri = data.getData(); 


     mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri); 
     } 
} 

'你將以位圖圖像的形式獲取圖像。轉換該位圖圖像到ByteArray []:

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
mBitmap .compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

然後取一列與blob數據類型的數據庫,然後插入在該列中這個字節陣列;

db.execSQL("create table Images(id integer, imageData blob);"); 

用於插入

ContentValues values = new ContentValues(); 
values.put("id", imageIdValue); 
values.put("imageData ", byteArray); 
database.insert("Image", null, values); 

我希望這會幫助你。

+0

謝謝@Prashant這可以幫助我 –

+0

@praval很高興我的回答可以幫助你。 – Cool7

相關問題