微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何从库中存储SQLite数据库中的图像

我已尝试此代码将图像从库上传到我的应用程序中的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); 
}}

解决方法:

通过按钮单击导入表单设备

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐