【问题标题】:Error: Cannot attach empty file in GMAIL app using File provider错误:无法使用文件提供程序在 GMAIL 应用程序中附加空文件
【发布时间】:2017-04-10 06:23:41
【问题描述】:

我正在尝试在 gmail 应用程序中附加 pdf 文件。我已阅读thisthis(应用解决方案)我正在尝试;

public static void attachFile(Context ctx) {
    String TAG = "Attach";
    File documentsPath = new File(ctx.getFilesDir(), "documents");
    Log.i(TAG,"documentsAbsolutePath Output");
    Log.i(TAG, documentsPath.getAbsolutePath().toString());
    File file = new File(documentsPath, "sample.pdf");
    if ( file.exists() ) {
        Toast.makeText(ctx, "Exits", Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(ctx, "Not Exist", Toast.LENGTH_LONG).show();
    }
    Log.i(TAG,"file Output");
    Log.i(TAG, file.toString());
    Log.i(TAG, String.valueOf(file.length()));
    Uri uri = FileProvider.getUriForFile(ctx, "com.example.fyp_awais.attachfiletest2.fileprovider", file);
    Log.i(TAG,"URI Output");
    Log.i(TAG,uri.toString());
    Intent intent = ShareCompat.IntentBuilder.from((Activity) ctx)
            .setType("application/pdf")
            .setStream(uri)
            .setChooserTitle("Choose bar")
            .createChooserIntent()
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    ctx.startActivity(intent);
}

输出

documentsAbsolutePath Output
/data/data/com.example.fyp_awais.attachfiletest2/files/documents
 file Output
/data/data/com.example.fyp_awais.attachfiletest2/files/documents/sample.pdf
 0
 URI Output
 content://com.example.fyp_awais.attachfiletest2.fileprovider/pdf_folder/sample.pdf

菜单

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.fyp_awais.attachfiletest2.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepath" />
</provider>

文件路径.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <files-path name="pdf_folder" path="documents/"/>
    </paths>
</PreferenceScreen

一个pdf文件保存在Galaxy Core Prime\Phone\documents中。 (文件大小:53.7KB)

但它给了

无法附加空文件。

我对@9​​87654333@ 这一行中的文件夹名称感到困惑。该文件位于\Phone\documents 中。那么为什么是文件夹名称?

编辑 1

尝试将setType(application/pdf) 替换为setType("message/rfc822"),但没有成功。有什么帮助吗?

【问题讨论】:

  • 尝试用这个setType("message/rfc822")替换setType
  • @FAЯAƸ 试过了。它不起作用。
  • 检查thisthis的答案。
  • @FAЯAƸ 我正在尝试使用FileProvider 来执行此操作。用于与其他应用共享文件。
  • File file = new File(documentsPath, "sample.pdf");。之后添加以下代码:if ( ! file.exists() ) {Toast ( ... file.. file.getAbsolutePath() ... not found...); return;}。请告诉结果。

标签: android gmail


【解决方案1】:

以 URI 格式发送文件,如下所示:

Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("application/image");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
        emailIntent.putExtra(Intent.EXTRA_CC, CC);
ArrayList<Uri> uris = new ArrayList<>();
        //convert from paths to Android friendly Parcelable Uri's
        uris.add(frontImageUri);
        uris.add(backImageUri);
        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

【讨论】:

  • 是的,我就是这样做的,在 uris 中也有一大堆文件。您不能将文件保存在私有应用程序存储或其他地方,所以我只需在下载目录中创建一个副本并发送那个
【解决方案2】:

如果可以发邮编就试试这种方式

 String fileNameStor_zip = Environment.getExternalStorageDirectory() + "/" + fileName + ".zip";

String[] path = { your 1st pdf  File Path, your 2nd pdf  File Path};

Compress compress = new Compress(path, fileNameStor_zip);

compress.zip();


URI =  Uri.parse("file://" + fileNameStor_zip);

提供您的 Gmail Intent

intent.putExtra(Intent.EXTRA_STREAM, URI);

【讨论】:

    【解决方案3】:
    Uri contentUri = FileProvider.getUriForFile(this, "com.mydomain.fileprovider", newFile);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    i.setData(contentUri);
    

    【讨论】:

      【解决方案4】:
      String filename="my_file.vcf"; 
      File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
      Uri path = Uri.fromFile(filelocation); 
      Intent emailIntent = new Intent(Intent.ACTION_SEND);
      // set the type to 'email'
      emailIntent .setType("vnd.android.cursor.dir/email");
      String to[] = {"asd@gmail.com"};
      emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
      // the attachment
      emailIntent .putExtra(Intent.EXTRA_STREAM, path);
      // the mail subject
      emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
      startActivity(Intent.createChooser(emailIntent , "Send email..."));
      

      【讨论】:

        【解决方案5】:

        您必须授予访问 gmail 存储空间的权限。

        【讨论】:

          【解决方案6】:

          如果它有帮助 - 这是我在带有几个文件的应用程序调试功能中所做的,它应该没有任何不同。 在附加它们之前,我首先将它们复制/导出到用户公共文件夹中,并使它们在世界范围内可读。

           if (verifyStoragePermissions(c)) {
                  final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                  intent.setType("text/plain");
                  intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipient@email.address"});
                  intent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
          
                  //build up message
                  ArrayList<Uri> uris = new ArrayList<>();
                  StringBuilder content = new StringBuilder();
                  content.append(
                          String.format(
                                  c.getString(R.string.message_log_upload_email_body) +
                                          "\n\nmodelDesc:%s \n\nmanuf:%s \n\naId: %s,\n\nhIid: %s,\n\nfbId: %s.",
                                  Build.MODEL, Build.MANUFACTURER, getSomeVar1(), getSomeVar2(), getSomeVar3())
                  );
                  content.append("\n\nMy logged in account id is: ").append(getAccountId(c)).append(".");
                  content.append("\n\nHere's an overview of my attachments:\n");
          
                  for (String s : addresses) {
                      File f = new File(s);
                      //noinspection ResultOfMethodCallIgnored
                      f.setReadable(true, false);
                      Uri add = Uri.fromFile(f);
                      //add attachment manifest
                      content.append(String.format(Locale.UK, "|->  %s (%.3f kb)\n", f.getName(), (float) f.length() / 1024));
                      uris.add(add);
                  }
                  //attach the things
                  intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
                  //set content/message
                  intent.putExtra(Intent.EXTRA_TEXT, content.toString());
                  c.startActivity(intent);
              }
          }
          

          addresses 是该函数的 String[] 参数。我通过使用于导出文件的函数返回它们导出的文件地址的字符串数组来构建它 - 因为我最初将文件存储在我的应用程序的私有存储中

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-12
            • 1970-01-01
            • 1970-01-01
            • 2011-08-27
            相关资源
            最近更新 更多