我正在尝试将元数据添加到 android 应用程序中的图像。应用程序从图库接收图像,并可以使用共享按钮成功转发它。然而,我正在努力做的是将元数据添加到图像中。我只需要添加一些简单的数据(主要是描述)。
oid handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
Bitmap myBitmap;
try {
myBitmap = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), imageUri);
myBitmap = Bitmap.createScaledBitmap(myBitmap,
myBitmap.getWidth() / 4, myBitmap.getHeight() / 4,
false);
ImageView myImage = (ImageView) findViewById(R.id.image);
myImage.setImageBitmap(myBitmap);
myImage.refreshDrawableState();
Button shareButton = (Button) findViewById(R.id.button);
shareButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (myBitmap != null) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
bytes);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "myfirstpicture.jpg");
values.put(Images.Media.DESCRIPTION,
"A pretty picture");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.ORIENTATION, 0);
values.put(Images.Media.DATA, bytes.toByteArray());
// Uri i =
String path = Images.Media.insertImage(
getApplicationContext()
.getContentResolver(), myBitmap,
"pic.jpg", "a pretty picture");
Uri i = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, i);
startActivity(Intent.createChooser(share,
"Share Image"));
} else {
//Display message
}
}
});
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这允许我发送图像,但元数据丢失并且标题重置为时间戳。我尝试使用不同的 URI
Uri i = getApplicationContext().getContentResolver().insert(Images.Media.INTERNAL_CONTENT_URI, values);
但这会导致应用程序崩溃(下面的 logcat)。
我环顾四周,但找不到任何有效的方法。
非常感谢帮助
更新:备用 URI 的 Logcat 错误
05-21 15:44:11.270: E/AndroidRuntime(7356): FATAL EXCEPTION: main
05-21 15:44:11.270: E/AndroidRuntime(7356): java.lang.IllegalStateException: Unable to create new file: [B@426df408
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.os.Parcel.readException(Parcel.java:1439)
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:440)
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.content.ContentResolver.insert(ContentResolver.java:917)
05-21 15:44:11.270: E/AndroidRuntime(7356): at com.bham.cs.blindrouting.MainActivity$1.onClick(MainActivity.java:128)
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.view.View.performClick(View.java:4476)
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.view.View$PerformClick.run(View.java:18795)
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.os.Handler.handleCallback(Handler.java:730)
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.os.Looper.loop(Looper.java:177)
05-21 15:44:11.270: E/AndroidRuntime(7356): at android.app.ActivityThread.main(ActivityThread.java:5493)
05-21 15:44:11.270: E/AndroidRuntime(7356): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 15:44:11.270: E/AndroidRuntime(7356): at java.lang.reflect.Method.invoke(Method.java:525)
05-21 15:44:11.270: E/AndroidRuntime(7356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
05-21 15:44:11.270: E/AndroidRuntime(7356): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
05-21 15:44:11.270: E/AndroidRuntime(7356): at dalvik.system.NativeStart.main(Native Method)