0

res/drawable フォルダにある png ファイルを電子メールの添付ファイルとして送信しようとしています。ファイルを添付して電子メールに含めることはできますが、.png ファイル拡張子がありません。ファイル名に拡張子を含めたい。誰か助けてくれませんか?これが私のコードです:

            Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
            i.putExtra(Intent.EXTRA_TEXT   , "Text");
            ArrayList<Uri> uris = new ArrayList<Uri>();
            Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.png_image);
            uris.add(newPath);

            i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(ShareActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
4

2 に答える 2

0

このメソッドを使用して、ファイルの拡張子を取得します

public static String getMimeType(Context context, Uri uri) {
    String extension;

    //Check uri format to avoid null
    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        //If scheme is a content
        final MimeTypeMap mime = MimeTypeMap.getSingleton();
        extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
    } else {
        //If scheme is a File
        //This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
        extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());

    }

    return extension;
}
于 2016-08-25T09:20:05.330 に答える
0

タイプを次のように変更します。

i.setType("image/png");

putParcelableArrayListExtra()は必要ではないと信じていますが。putExtra()仕事をするべきです。

于 2012-08-27T05:49:15.187 に答える