Java 只想在我手机的多媒体资料数据库中添加图像,但其间遇到了一些问题
我是Android Studio的新手。我正在尝试将手机图库中的图像添加到应用程序的数据库中。尝试了不同的方法,但没有成功 下面是应用程序的XML文件 activity_main.xml 应用程序的主java文件 MainActivity.javaJava 只想在我手机的多媒体资料数据库中添加图像,但其间遇到了一些问题,java,android,sqlite,Java,Android,Sqlite,我是Android Studio的新手。我正在尝试将手机图库中的图像添加到应用程序的数据库中。尝试了不同的方法,但没有成功 下面是应用程序的XML文件 activity_main.xml 应用程序的主java文件 MainActivity.java 在活动中,您需要获得用户选择的图像的结果。您已经在startActivityForResult上有了一个良好的开端。比如说: @Override protected void onActivityResult(int requestCode, in
在活动中,您需要获得用户选择的图像的结果。您已经在startActivityForResult上有了一个良好的开端。比如说:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_LOAD_IMAGE && resultCode == AppCompatActivity.RESULT_OK) {
Uri selectedImage = data.getData();
...
}
super.onActivityResult(requestCode, resultCode, data);
}
这为您提供了一个可以使用的图像URI,您可以将其存储在数据库中。否则,如果必须存储base64,则可以获得如下结果位图:
public Bitmap getBitmapFromUri(Context context, Uri selectedImage) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn,
null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return (getBitmapFromFileWithCorrectOrientation(picturePath, 4));
}
public Bitmap getBitmapFromFileWithCorrectOrientation(String filePath, int inSampleSize) {
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
bounds.inSampleSize = inSampleSize;
BitmapFactory.decodeFile(filePath, bounds);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = inSampleSize;
Bitmap bm;
try {
bm = BitmapFactory.decodeFile(filePath, opts);
} catch (OutOfMemoryError e) {
BitmapFactory.Options opt2 = new BitmapFactory.Options();
opt2.inSampleSize = inSampleSize * 2;
bm = BitmapFactory.decodeFile(filePath, opt2);
}
int orientation = ExifInterface.ORIENTATION_NORMAL;
try {
ExifInterface exif = new ExifInterface(filePath);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
} catch (IOException e) {
}
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
return Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
}
。。。。然后像这样的base64代码
public String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
它不是世界上最好的代码,但它可以满足您的需要 什么有效,什么无效?仅仅看一堆代码是很难分辨的我只是想知道如何将图像插入数据库。我陷入了无法将图像存储在数据库中的困境。将它们存储到文件系统,并将它们的路径保存在数据库中。记住使用合适的样本大小。如果用户选择了一个大得离谱的图像,那么当您尝试将Base64存储到数据库中时会遇到异常。你也不想选择太大的样本大小,否则你的图像在再次加载时会显得有点糟糕。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_LOAD_IMAGE && resultCode == AppCompatActivity.RESULT_OK) {
Uri selectedImage = data.getData();
...
}
super.onActivityResult(requestCode, resultCode, data);
}
public Bitmap getBitmapFromUri(Context context, Uri selectedImage) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn,
null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return (getBitmapFromFileWithCorrectOrientation(picturePath, 4));
}
public Bitmap getBitmapFromFileWithCorrectOrientation(String filePath, int inSampleSize) {
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
bounds.inSampleSize = inSampleSize;
BitmapFactory.decodeFile(filePath, bounds);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = inSampleSize;
Bitmap bm;
try {
bm = BitmapFactory.decodeFile(filePath, opts);
} catch (OutOfMemoryError e) {
BitmapFactory.Options opt2 = new BitmapFactory.Options();
opt2.inSampleSize = inSampleSize * 2;
bm = BitmapFactory.decodeFile(filePath, opt2);
}
int orientation = ExifInterface.ORIENTATION_NORMAL;
try {
ExifInterface exif = new ExifInterface(filePath);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
} catch (IOException e) {
}
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
return Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
}
public String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}