【发布时间】:2021-12-19 07:15:43
【问题描述】:
这就是我目前正在努力解决的问题。我正在使用以下代码将相机拍摄的图像保存到我的 SQLite 数据库中:
数据库数据库:
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " double , " +
FIELD_LAT + " double , " +
FIELD_ZOOM + " text , " +
FIELD_IMG + " text " +
" ) ";
db.execSQL(sql);
}
然后在相机意图中(Main Activity)
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Intent getCameraImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"MapImages/");
else
cameraFolder= context.getFilesDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File photo = new File(cameraFolder, "MapImages/" + imageFileName + ".jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
Uri.fromFile(photo);
startActivityForResult(getCameraImage, TAKE_PICTURE);
drawMarker(point);
ContentValues contentValues = new ContentValues();
contentValues.put(LocationsDB.FIELD_LAT, point.latitude);
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
contentValues.put(LocationsDB.FIELD_ZOOM, mMap.getCameraPosition().zoom);
contentValues.put(FIELD_IMG, String.valueOf(photo));
LocationInsertTask insertTask = new LocationInsertTask();
insertTask.execute(contentValues);
所以当我查看数据库时,它会将图像文件位置保存为:
/storage/emulated/0/MapImages/MapImages/JPEG_20211105_091000_.jpg
所以一切都很好!但是我现在如何检索该图像并将其显示在 ImageView 中?
我已经尝试过了(在 onloadFinished 中)
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
int locationCount = 0;
double lat = 0;
double lng = 0;
float zoom = 0;
double img = 0;
locationCount = arg1.getCount();
arg1.moveToFirst();
for (int i = 0; i < locationCount; i++) {
lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
img = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_IMG));
LatLng location = new LatLng(lat, lng);
drawMarker(location);
arg1.moveToNext();
}
if (locationCount > 0) {
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lng)));
mMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
Bitmap myBitmap = BitmapFactory.decodeFile(String.valueOf(img));
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
}
}
但随后收到以下错误:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: 0.0: open failed: ENOENT (No such file or directory)
我不想将图像保存为blob,因为这是不好的做法,因此我将文件路径保存到数据库
如果有人可以帮助指导我从数据库中检索文件并显示在我的ImageView
谢谢大家!
【问题讨论】:
-
I am saving the image taken by the camera to my SQLite database by using this code:抱歉,我看到你什么也没保存。并且表 FIELD_IMG 不能保存图像文件,只能保存文本。所以可能只有一个文件路径。如果是这样,那么给该字段一个更好的、不易混淆的名称。请详细说明并编辑您的帖子以解决此困惑。 -
嗨,它以以下格式将图像文件路径保存到该 FIELD_IMG
/storage/emulated/0/MapImages/MapImages/JPEG_20211105_091000_.jpgSi 我可以看到正在保存的文件。我只是无法检索要在ImageView中显示的文件。 FIELD_IMG 是text而不是 blob -
Please edit your post to solve this confusion我们等着你。 -
很抱歉让您感到困惑,但问题很清楚。 1. 将相机拍摄的图像保存到 SQLite 数据库 2. 图像文件路径(不是 blob)以这种格式保存到表中
/storage/emulated/0/MapImages/MapImages/JPEG_20211105_091000_.jpg3. 无法从表中检索图像文件以显示在ImageView