你好。我在将可绘制的 vector 图像附加到电子邮件时遇到问题。执行显示无法附加空文件。
public void enviarCorreu(View view) {
EditText t = findViewById(R.id.editText);
String email = t.getText().toString();
Drawable drawable = this.getResources().getDrawable(R.drawable.face);
Intent intent = new Intent(Intent.ACTION_SEND);
File f = new File(Uri.parse("android.resource://"+"com.example.m8_activitat2_tematicalliure/"+drawable).toString());
intent.putExtra(Intent.EXTRA_EMAIL,new String[]{email});
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_SUBJECT,email);
intent.putExtra(Intent.EXTRA_TEXT,"Missatge enviat de la imatge");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(f));
startActivity(Intent.createChooser(intent,"Enviant Correu ... "));
}
最佳答案
问题 #1:您使用了错误的 Uri
方案。
The documentation for EXTRA_STREAM
状态:
A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
您使用的是 android.resource:
方案,而不是 content:
。因此,一些尝试响应您的 Intent
的应用将不知道如何处理您的 Uri
。
问题#2:您没有提供具体的 MIME 类型。
你有:
intent.setType("image/*");
这是无效的。这是您的内容,因此您必须提供 MIME 类型。
<小时/>问题#3:没有表示 vector 可绘制资源的 MIME 类型。
您无法告诉接收应用程序其格式是什么,因为此格式不是为在应用程序之间传递而设计的。
<小时/>如果您希望将 ACTION_SEND
与 EXTRA_STREAM
一起使用:
使用
ContentProvider
(例如FileProvider
)来提供您的内容,以便您获得content:
Uri
在EXTRA_STREAM
中使用
让该内容属于可识别的 MIME 类型,并在
Intent
中使用该 MIME 类型(例如image/jpeg
、image/png
)
关于java - 无法在 Android Studio 上将可绘制的 vector 图像附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58461801/