如何存储图像从画廊SQLite数据库

编程入门 行业动态 更新时间:2024-10-26 00:24:21
本文介绍了如何存储图像从画廊SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经试过这种code,从库上传图像sqllite数据库在我的应用程序,但是当我的应用程序试图打开图库它给力要关闭的错误,我不知道什么是problem.help我,感谢名单在高级

公共类ImagggggggActivity延伸活动{     私有静态最终诠释SELECT_PICTURE = 1;     私人字符串selectedImagePath;     私人ImageView的IMG;     公共无效的onCreate(包savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.main);          ((按钮)findViewById(R.id.button1))          .setOnClickListener(新OnClickListener(){              公共无效的onClick(查看为arg0){                  //中的onCreate或任何情况下,你的希望用户                  //选择一个文件                  意向意图=新的意图();                  intent.setType(图像/ *);                  intent.setAction(Intent.ACTION_GET_CONTENT);                  startActivityForResult(Intent.createChooser(意向,                          选择图片),SELECT_PICTURE);              }          }); } 公共无效onActivityResult(INT申请code,INT结果code,意图数据){  如果(结果code == RESULT_OK){      如果(要求code == SELECT_PICTURE){          乌里selectedImageUri = data.getData();          selectedImagePath = getPath(selectedImageUri);      }  } } 公共字符串getPath(URI URI){  的String []投影= {MediaStore.Images.Media.DATA};  光标光标= managedQuery(URI,投影,NULL,NULL,NULL);  INT与Column_Index =光标          .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  cursor.moveToFirst();  返回cursor.getString(Column_Index中); }}

解决方案

按按钮,点击导入表单设备

意图sdintent =新的意图(Intent.ACTION_PICK); sdintent.setType(图像/ *); startActivityForResult(sdintent,SD_REQUEST);

获取图像格式的SD卡

如果(要求code == SD_REQUEST){ 乌里selectedImage = data.getData(); 的String [] filePathColumn = {MediaStore.Images.Media.DATA}; 光标光标= getContentResolver()查询(selectedImage,                 filePathColumn,NULL,NULL,NULL); cursor.moveToFirst(); INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]); 字符串文件路径= cursor.getString(参数:columnIndex); cursor.close(); 位图yourSelectedImage = BitmapFactory.de codeFILE(文件路径); testimage.setImageBitmap(yourSelectedImage); ByteArrayOutputStream流=新ByteArrayOutputStream(); yourSelectedImage preSS(Bitmap.Com pressFormat.PNG,100,流); 的字节数组= stream.toByteArray(); }

保存图像

DatabaseAdapter dbHelper =新DatabaseAdapter(Profiles.this);        dbHelper.open();        dbHelper.createUserProfiles(字节阵列);        dbHelper.close();

现在在DatabaseAdapter.java

定义

公共静态最后弦乐U_PIC =图片;

然后

专用长createUserTableContentValues​​(长ID,byte []的byteImage){         ContentValues​​值=新ContentValues​​();         values​​.put(ID,ID);         values​​.put(U_PIC,byteImage); 返回database.insert(IMAGE_INSERT,空,价值观); }

我觉得这可能是帮助你......

其他资源: www.helloandroid/tutorials/store-imagesfiles-database

HTTP://www.$c$ cranch/t/507054/Android/Mobile/Storing-image-database

hi.baidu/_java/blog/item/ 971e142a13afe6305243c12f.html

的www.tutorialforandroid/2009/10/how-to-insert-image-data-to-sqlite.html

i have tried this code to upload an image from gallery to sqllite database in my application but when my application tries to open gallery it gives FORCE TO CLOSE Error and i dont know what is the problem.help me and thanx in advanced

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

解决方案

Import form device by button click

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

Get image form sd card

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(); yourSelectedImagepress(Bitmap.CompressFormat.PNG, 100, stream); byteArray = stream.toByteArray(); }

Save Image

DatabaseAdapter dbHelper = new DatabaseAdapter(Profiles.this); dbHelper.open(); dbHelper.createUserProfiles( byteArray); dbHelper.close();

NOw in DatabaseAdapter.java

Define

public static final String U_PIC = "picture";

Then

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

I think this might be help you.....

Other resources: www.helloandroid/tutorials/store-imagesfiles-database

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

hi.baidu/_java/blog/item/971e142a13afe6305243c12f.html

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

更多推荐

如何存储图像从画廊SQLite数据库

本文发布于:2023-10-07 10:43:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469175.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:画廊   图像   数据库   SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!