Java Can´;t在Android Studio上将矢量图像从drawable附加到电子邮件

Java Can´;t在Android Studio上将矢量图像从drawable附加到电子邮件,java,android,android-intent,Java,Android,Android Intent,你好。我在将矢量图像从drawable附加到电子邮件时遇到问题。执行说明无法附加空文件。 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

你好。我在将矢量图像从drawable附加到电子邮件时遇到问题。执行说明无法附加空文件。

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
方案

国家:

内容:一个URI,包含与意图相关联的数据流,与操作_SEND一起使用,以提供正在发送的数据

您使用的是
android。资源:
scheme,它不是
content:
。因此,一些试图响应您的
意图的应用程序将不知道如何处理您的
Uri


问题2:您没有提供具体的MIME类型

你有:

intent.setType("image/*");
这是无效的。它是您的内容,因此您必须提供MIME类型


问题3:没有表示向量可绘制资源的MIME类型

您无法告诉接收应用程序这是什么格式,因为此格式不是为在应用程序之间传递而设计的


如果希望将
操作发送
额外流一起使用

  • 使用
    ContentProvider
    ,例如
    FileProvider
    ,为您的内容提供服务,这样您就可以获得
    内容:
    Uri
    ,以便在
    额外流中使用

  • 使该内容具有可识别的MIME类型,并在
    意图中使用该MIME类型(例如,
    图像/jpeg
    图像/png