我想将一些图像存储在数据库中,稍后恢复它们并在自定义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;
//将照片设置为按钮/ 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);
}
答案 0 :(得分:3)
嗯,我在数据库中存储图像的建议是:不要。 它没有效果,最好使用路径存储它们。这在this question上进行了讨论。
但是。如果你真的因为某种原因存储它。尝试将图像编码为Base64 String
,并将其存储在数据库中。
Android为此提供了Base64 class
。尝试使用以下代码段进行编码:
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.
您可以查看Bitmap
课程here的文档。
答案 1 :(得分:0)
1.将图像转换或编码为base64字符串,并将其作为字符串存储在数据库中。从数据库中读取时,将其再次解码为图像。 2.将图像转换为bytearray并将其存储在DB中。