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

2023-09-06 07:15:28 作者:﹏我、没有错

我已经试过这种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);
 
SQL server2000怎样存储图片数据 把图片的地址存放在数据库 这个该如何做 急求

获取图像格式的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.com 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,空,价值观);
}
 

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

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

HTTP://www.$c$ cranch.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

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();
yourSelectedImage.compress(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: 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