【问题标题】:How to store image in SQLite database from gallery如何从图库中将图像存储在 SQLite 数据库中
【发布时间】:2012-06-14 05:01:26
【问题描述】:

我已经尝试使用此代码将图像从画廊上传到我的应用程序中的 sqllite 数据库,但是当我的应用程序尝试打开画廊时,它给出了强制关闭错误,我不知道是什么问题。请帮助我和thanx高级

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); 
}}

【问题讨论】:

    标签: android image sqlite


    【解决方案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

    【讨论】:

      【解决方案2】:

      使用 BLOB 数据类型,我们可以将图像存储在 SQLite 数据库中。实际存储在数据库中的数据是构成图像或文件的字节。

      至于从图库中获取图片,看here,你好像忘记了“超级”调用。

      【讨论】:

      • 你能给我看一个在android应用程序中完成的例子吗?
      • 嗯。看来,您的问题根本不是将图像存储到 SQLite 中,而是从图库中获取它。我认为,你的问题应该根据这个来编辑。答案已编辑。
      【解决方案3】:
      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(); 
      } 
      

      【讨论】:

        猜你喜欢
        • 2012-03-10
        • 2012-06-04
        • 1970-01-01
        • 2021-09-13
        • 2013-10-04
        • 2018-07-22
        • 1970-01-01
        相关资源
        最近更新 更多