대용량 파일 (비트 맵)을 컨텐츠 공급자에 저장하려고 시도하지만 어떻게 작동하는지 모릅니다. 내 레코드에 "_data"필드를 추가하고 내 콘텐츠 공급자의 openFile() 메서드를 재정의했습니다."_ 데이터"필드를 처리하는 방법은 무엇입니까?
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table contact (_id integer primary key autoincrement, NAME text collate nocase, IMAGE text, _data text);");
}
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
String rowID = uri.getPathSegments().get(1);
String dir = Environment.DIRECTORY_PICTURES;
File file = new File(getContext().getExternalFilesDir(dir), rowID);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
int fileMode = 0;
if (mode.contains("w"))
fileMode |= ParcelFileDescriptor.MODE_WRITE_ONLY;
if (mode.contains("r"))
fileMode |= ParcelFileDescriptor.MODE_READ_ONLY;
if (mode.contains("+"))
fileMode |= ParcelFileDescriptor.MODE_APPEND;
return ParcelFileDescriptor.open(file, fileMode);
}
저는 리졸버의 openOutputStream() 메소드를 사용하여 비트 맵을 삽입하고 있습니다.
ContentValues values = new ContentValues();
values.put("NAME", "abc");
Uri rowUri = cr.insert(MyContentProvider.CONTENT_URI, values);
values.put("IMAGE", rowUri.toString());
cr.update(rowUri, values, null, null);
InputStream inputStream = httpConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://www.xyz.com/abc.jpg").getContent());
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, cr.openOutputStream(rowUri));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
이렇게하면 파일은 내 SD 카드에 저장되지만 "_ 데이터"필드는 비어있게됩니다. 내가 뭘 잘못하고있어?
"_data"입력란을 직접 작성해야합니까? this tutorial에 따르면 대답은 "예"인 것으로 보입니다. "_data"필드는 해당 파일에 대한 장치의 정확한 파일 경로와 함께 insert() 메서드 내에 작성됩니다. 레코드 내의 URI 참조 IMAGE는 필요하지 않습니다. 그러나이 튜토리얼은 이미 몇 년 전의 일이며, "_data"가 이와 같이 직접 작성된 다른 예제를 발견하지 못했습니다. 누군가 좋은 예를 알고 있습니까?