【问题标题】:How to store the image in database and shows in listview如何将图像存储在数据库中并在列表视图中显示
【发布时间】:2015-09-08 08:56:15
【问题描述】:

我想在数据库中存储一些图像,稍后恢复它们并在自定义ListView 上显示它们。我该怎么做?

这是我到目前为止所做的:

//here, we are making a folder named picFolder to store pics taken by the camera using this application
dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath() + "/picFolder/"; 
    File newdir = new File(dir); 
    if(!newdir.exists()){
        newdir.mkdirs();
    }


//Switch cases are 

案例 R.id.btnPhotoGallary2:

        Intent gallaryIntent = new Intent(Intent.ACTION_PICK,
                   android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(gallaryIntent , TAKE_GALLARY_CODE);//one can be replced with any action code
        break;

    case R.id.btnPhotoCamera1:
        count++;
        String file = dir+count+".jpg";
        File newfile = new File(file);
        try {
            newfile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }       

        capturedPhotoUri = Uri.fromFile(newfile);

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedPhotoUri);

        startActivityForResult(cameraIntent, TAKE_CAMERA_CODE);
        break;

// 将照片设置为Button/ImageView

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 0://Captured photo from Camera
        if(resultCode == RESULT_OK){  
            String pathNamePhoto = capturedPhotoUri.getPath().toString();
            System.out.println("Photo Path Is :"+pathNamePhoto);
            d = BitmapDrawable.createFromPath(pathNamePhoto);
            ivProfile.setImageDrawable(d);
            Log.d("CameraDemo", "Pic saved");
            Log.v("CapturedPhoto PATH ==>> ",pathNamePhoto);
            pd.setProfImagepath(pathNamePhoto);
        }
        dialog.dismiss();
        break;


    case 1://Select Image from Gallery
        if(resultCode == RESULT_OK){  
            Uri selImageUri = data.getData();
            String pathNameImage = getPath(selImageUri);
            System.out.println("Image Path Is :"+pathNameImage);

            ivProfile.setImageURI(selImageUri);
            Log.d("GallaryDemo", "Get Pic ");
            Log.v("IMAGE PATH ==>> ",pathNameImage);
            pd.setProfImagepath(pathNameImage);
        }          
        dialog.dismiss();
        break;
    }

}

//获取图像的真实路径

    @SuppressWarnings("deprecation")
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 database sqlite listview


    【解决方案1】:

    好吧,我对在数据库中存储图像的建议是:不要。 效果不好,最好使用路径存储。这已在this question 上讨论过。

    但是。如果您出于某种原因真的要存储它。尝试将图像编码为 Base64 String,并将其存储在您的数据库中。

    Android 为此提供了Base64 class。尝试使用以下sn-p进行编码:

    Bitmap bitmap = BitmapFactory.decodeFile("image.jpg");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); // Could be Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.WEBP
    byte[] bai = baos.toByteArray();
    
    String base64Image = Base64.encodeToString(bai, Base64.DEFAULT);
    
    // Call your method to save this string on the DB here.
    

    您必须对其进行解码尝试以下操作:

    byte[] data = Base64.decode(base64Image, Base64.DEFAULT);
    Bitmap bm;
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inMutable = true;
    bm = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
    
    // Now do whatever you want with the Bitmap.
    

    您可以查看Bitmaphere 的文档。

    【讨论】:

      【解决方案2】:

      1.将图像转换或编码为base64字符串并将其作为字符串存储在数据库中。当您从数据库中读取时,再次将其解码为图像。 2. 将图片转换为bytearray并存入DB。

      【讨论】:

        猜你喜欢
        • 2011-10-25
        • 2011-12-28
        • 1970-01-01
        • 1970-01-01
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多