gpt4 book ai didi

android - 如何处理 "_data"字段?

转载 作者:行者123 更新时间:2023-11-30 03:52:48 27 4
gpt4 key购买 nike

我正在尝试在我的 Content Provider 中存储大文件(位图),但不知道如何操作。我在我的记录中添加了一个“_data”字段,并覆盖了我的 Content Provider 的 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”字段保持为空。我做错了什么?

“_data”字段需要自己写吗?根据this tutorial 答案似乎是"is"。 “_data”字段在 insert() 方法中写入,其中包含该文件在设备上的确切文件路径。记录中的 URI 引用 IMAGE 不是必需的。但是这个教程已经有些年头了,我还没有找到另一个像这样直接写“_data”的例子。有人知道一个很好的例子吗?

最佳答案

上述教程中建议的解决方案有效。

关于android - 如何处理 "_data"字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13896419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com