有意添加附件到gmail应用程序

时间:2016-07-01 10:57:48

标签: android

我有一个应用程序打开pdf文件,点击应用程序(原始文件夹中的默认PDF),openwith(myapp)以及邮件附件。我将选定的PDF文件转换为输入流,然后转换为bytearray。现在我有一个按钮,当用户点击它时,需要使用意图通过gmail应用程序发送电子邮件。我需要将inputstream或bytearray作为pdffile添加到邮件作为attachment.I使用以下代码我可以看到没有字节的附件和收到的电子邮件donot有附件。苦苦挣扎超过10个小时..请帮忙...

     Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                sendIntent.setType("application/pdf");
                sendIntent.setData(Uri.parse("sample@gmailcom"));
                sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
                sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "sample@gmail.com" });
                sendIntent.putExtra(Intent.EXTRA_SUBJECT, "testPDF");
                sendIntent.putExtra(Intent.EXTRA_TEXT, "this is a PDF ");


                File fileIn = new File(name, "myfile.pdf");
                Toast.makeText(this, fileIn.getName(), Toast.LENGTH_LONG).show();
                Uri fileuri = Uri.fromFile(fileIn);
                sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileuri.toString()));

                startActivity(sendIntent);

2 个答案:

答案 0 :(得分:1)

我尝试了很多方法,最后自己找到了解决方案。使用文件提供程序将文件附加到邮件而不将其物理存储在设备上。代码如下,希望它可以帮助别人。 使用以下代码添加一个AttachFileProvider类     公共类AttachFileProvider扩展ContentProvider {     private static final String CLASS_NAME =“AttachFileProvider”;

int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };
var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);
foreach (var item in numbersAndWords)
Console.WriteLine(item);

}

附加邮件使用以下代码。

// The authority is the symbolic name for the provider class
public static final String AUTHORITY = "com.example.pDoc.pdocsigner.provider";

// UriMatcher used to match against incoming requests<br />
private UriMatcher uriMatcher;
@Override
public boolean onCreate() {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    // Add a URI to the matcher which will match against the form
    uriMatcher.addURI(AUTHORITY, "*", 1);

    return true;
}


@Override
public ParcelFileDescriptor openFile(Uri uri,  String mode) throws FileNotFoundException {
    // Check incoming Uri against the matcher
    switch (uriMatcher.match(uri)) {
        // If it returns 1 - then it matches the Uri defined in onCreate
        case 1:
            // The desired file name is specified by the last segment of the path
            // Take this and build the path to the file
            String fileLocation = getContext().getCacheDir() + File.separator + uri.getLastPathSegment();

            // Create & return a ParcelFileDescriptor pointing to the file
            ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(fileLocation),  ParcelFileDescriptor.MODE_READ_ONLY);
            return pfd;

        // Otherwise unrecognised Uri
        default:
            //Log.v(LOG_TAG, "Unsupported uri: '" + uri + "'.");
            throw new FileNotFoundException("Unsupported uri: " + uri.toString());
    }
}

// //////////////////////////////////////////////////////////////
// Not supported / used / required for this example
// //////////////////////////////////////////////////////////////

@Override
public int update(Uri uri, ContentValues contentvalues, String s, String[] as) { return 0; }

@Override
public int delete(Uri uri, String s, String[] as) { return 0; }

@Override
public Uri insert(Uri uri, ContentValues contentvalues) { return null; }

@Override
public String getType(Uri uri) { return null; }

@Override
public Cursor query(Uri uri, String[] projection, String s, String[] as1, String s1) { return null; }

答案 1 :(得分:0)

您应该确保您的附件文件可以被gmail应用程序读取,因此请将其放在外部SD卡存储区域中。也许你的路径有问题,改变它并测试。