赞
踩
主要实现思路:
1、打开系统自带图库。代码如此:
需要提前设置imgRequestCode。
-
- private final int imgRequestCode=1123;
-
- private void selectPic() {
- Intent intent = new Intent();
- Operation opt=new Intent.OperationBuilder().withAction("android.intent.action.GET_CONTENT").build();
- intent.setOperation(opt);
- intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
- intent.setType("image/*");
- startAbilityForResult(intent, imgRequestCode);
- }
2、在自带图库关闭后重写onAbilityResult事件。
- @Override
- protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
- if(requestCode==imgRequestCode)
- {
- //选择的Img对应的Uri
- String chooseImgUri=resultData.getUriString();
-
- //定义数据能力帮助对象
- DataAbilityHelper helper=DataAbilityHelper.creator(getContext());
- //定义图片来源对象
- ImageSource imageSource = null;
- //获取选择的Img对应的Id
- String chooseImgId=null;
- //如果是选择文件则getUriString结果为content://com.android.providers.media.documents/document/image%3A30,其中%3A是":"的URL编码结果,后面的数字就是image对应的Id
- //如果选择的是图库则getUriString结果为content://media/external/images/media/30,最后就是image对应的Id
- //这里需要判断是选择了文件还是图库
- if(chooseImgUri.lastIndexOf("%3A")!=-1){
- chooseImgId = chooseImgUri.substring(chooseImgUri.lastIndexOf("%3A")+3);
- }
- else {
- chooseImgId = chooseImgUri.substring(chooseImgUri.lastIndexOf('/')+1);
- }
- //获取图片对应的uri,由于获取到的前缀是content,我们替换成对应的dataability前缀
- Uri uri=Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,chooseImgId);
- try {
- //读取图片
- FileDescriptor fd = helper.openFile(uri, "r");
- imageSource = ImageSource.create(fd, null);
- //创建位图
- PixelMap pixelMap = imageSource.createPixelmap(null);
- //设置图片控件对应的位图
- image.setPixelMap(pixelMap);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (imageSource != null) {
- imageSource.release();
- }
- }
- }
- }
3、保存数据库时需要将Image控件的图片转换成byte[]二进制数据。
- //将图片转换成byte[]
- public byte[] getBytes(Image image) {
- PixelMap pm = image.getPixelMap();
- ImagePacker imagePacker = ImagePacker.create();
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions();
- imagePacker.initializePacking(byteArrayOutputStream, packingOptions);
- imagePacker.addImage(pm);
- imagePacker.finalizePacking();
- byte[] bytes = byteArrayOutputStream.toByteArray();
- return bytes;
- }
4、保存数据库相关代码如下:
- private Image image = (Image)findComponentById(ResourceTable.Id_dingdan_tupian);
-
- saveButton.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
-
- byte[] tp = getBytes(image);
- String saveSQL = "insert into test " +
- "(rupiah) " +
- "values " +
- "(?)";
- store.executeSql(saveSQL,new Object[]{tp});
-
- }
- });
5、到此图片就保存到数据库中了。
以下是从数据库中取出并显示在Image控件中:
1、取出的数据为byte[],必须转换成PixelMap
- public PixelMap ByteToPixelMap(byte[] bytes){
- // 用于 ImageSource的 create(bytes,srcOpts)方法
- ImageSource.SourceOptions srcOpts=new ImageSource.SourceOptions();
- //设置图片原格式也可以用 null
- srcOpts.formatHint="image/jpg";
- ImageSource imageSource=ImageSource.create(bytes,srcOpts);
- //通过ImageSource创建 PixelMap文件
- PixelMap pixelMap=imageSource.createPixelmap(null);
- return pixelMap;
- }
2、将数据从数据库取出,并利用上述函数转换成PixelMap
- public PixelMap getPixelMap(String selectedID){
- String querySQL = "select blobType from test where id = ?";
- ResultSet rl = store.querySql(querySQL, new String[]{selectedID});
- byte[] getbyte = null;
- PixelMap pixelMap = null;
- while (rl.goToNextRow()) {
- getbyte = rl.getBlob(rl.getColumnIndexForName("blobType"));
- pixelMap = ByteToPixelMap(getbyte);
- }
- return pixelMap;
- }
3、将数据直接赋值给Image控件
- private Image image1 = (Image)findComponentById(ResourceTable.Id_dingdan_tupian1);
-
-
- queryButton.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- image1.setPixelMap(getPixelMap("1"));
- }
- });
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。