2012-06-11 195 views
1

我已經試過這段代碼從圖庫上傳圖像到我的應用程序中的sqllite數據庫,但是當我的應用程序試圖打開圖庫時,它給了FORCE TO CLOSE錯誤,我不知道什麼是problem.help我和thanx先進的如何在圖庫中存儲SQLite數據庫中的圖像

public class ImagggggggActivity extends Activity { 

    private static final int SELECT_PICTURE = 1; 
    private String selectedImagePath; 
    private ImageView img; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main); 

     ((Button) findViewById(R.id.button1)) 
     .setOnClickListener(new OnClickListener() { 

      public void onClick(View arg0) { 

       // in onCreate or any event where your want the user to 
       // select a file 
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent, 
         "Select Picture"), SELECT_PICTURE); 
      } 
     }); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (resultCode == RESULT_OK) { 
    if (requestCode == SELECT_PICTURE) { 
     Uri selectedImageUri = data.getData(); 
     selectedImagePath = getPath(selectedImageUri); 
    } 
} 
} 

public String getPath(Uri uri) { 
String[] projection = { MediaStore.Images.Media.DATA }; 
Cursor cursor = managedQuery(uri, projection, null, null, null); 
int column_index = cursor 
     .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
cursor.moveToFirst(); 
return cursor.getString(column_index); 
}} 

回答

1

進口形式通過按鈕裝置點擊

Intent sdintent = new Intent(Intent.ACTION_PICK); 
sdintent.setType("image/*"); 
startActivityForResult(sdintent, SD_REQUEST); 

獲取圖像格式的SD卡

if (requestCode == SD_REQUEST) { 
Uri selectedImage = data.getData(); 
String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
cursor.moveToFirst(); 

int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
String filePath = cursor.getString(columnIndex); 
cursor.close(); 

Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); 

testimage.setImageBitmap(yourSelectedImage); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byteArray = stream.toByteArray(); 
} 

保存圖像

DatabaseAdapter dbHelper = new DatabaseAdapter(Profiles.this); 

     dbHelper.open(); 
     dbHelper.createUserProfiles(byteArray); 
     dbHelper.close(); 

現在DatabaseAdapter.java

定義

public static final String U_PIC = "picture"; 

然後

private long createUserTableContentValues(long id,byte[] byteImage) { 
     ContentValues values = new ContentValues(); 
     values.put(ID, id); 
     values.put(U_PIC, byteImage); 
return database.insert(IMAGE_INSERT, null, values); 
} 

我認爲這可能是幫助你.....

其他資源: http://www.helloandroid.com/tutorials/store-imagesfiles-database

http://www.coderanch.com/t/507054/Android/Mobile/Storing-image-database

http://hi.baidu.com/_java/blog/item/971e142a13afe6305243c12f.html

http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html

1

使用BLOB數據類型,我們可以在SQLite數據庫中存儲圖像。實際存儲在數據庫中的數據是組成圖像或文件的字節。

至於從畫廊獲取圖片,看看here,看起來,你已經忘記了「超級」呼叫。

+0

你能請告訴我一個在android應用程序中完成的例子嗎? – Djkgotso

+0

嗯。看來,你的問題並不是將圖像存儲到SQLite中,而是從圖庫中獲取它。我認爲,你的問題應該根據這個編輯。回答編輯。 – Gangnus

+0

是的,這是我的問題 – Djkgotso

0
db.execSQL("CREATE TABLE images (" 
    + "_id INTEGER PRIMARY KEY AUTOINCREMENT," 
    + "data BLOB," 
    + "hash BLOB UNIQUE" 
    + ");"); 

將圖像轉換爲BLOB,使用下面的代碼:(注意 我們在這裏壓縮圖像)

private byte[] getBitmapAsByteArray(Bitmap bitmap) 
{ 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    // Middle parameter is quality, but since PNG is lossless, it doesn't matter 
    bitmap.compress(CompressFormat.PNG, 0, outputStream); 
    return outputStream.toByteArray(); 
} 
相關問題