我是Android Studio开发的新手,尝试将图片的路径保存到SQLite数据库时遇到了问题。 即使路径和文件与手机文件夹中的文件相同,我也会收到错误消息“ java.io.FileNotFoundException”, 我不明白为什么会有这个问题,有人可以向我解释我在想什么吗? java.io.FileNotFoundException Column_index:0 DataBaseHelper.java
public boolean InsertImage(File x){
SQLiteDatabase db = this.getWritableDatabase();
try {
FileInputStream fs = new FileInputStream(x);
byte [] imgbyte = new byte[fs.available()];
fs.read(imgbyte);
ContentValues contentValues = new ContentValues();
contentValues.put("img",imgbyte);
db.insert(TABLE_NAME_USERS,"Avatar",contentValues);
fs.close();
return true;
} catch (IOException e){
e.printStackTrace();
return false;
}
}
SignIn.java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
ImageView AvatarImage = findViewById(R.id.AvatarImage);
super.onActivityResult(requestCode, resultCode,data);
if(requestCode == REQUEST_CODE && data != null && data.getData() != null){
Uri uri = data.getData();
String path = uri.getPath(); // "/mnt/sdcard/FileName.mp3"
File file = new File((path));
String x = getPath(uri);
myDb.InsertImage(file);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
AvatarImage.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getPath(Uri uri){
if (uri==null)return null;
String [] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri,projection,null,null,null);
if (cursor!=null){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String dede = cursor.getString(column_index);
return cursor.getString(column_index);
}
return uri.getPath();
}