2011/05/16

GmailのContentProviderから添付ファイル取得

GmailのContentProviderから添付ファイルを取得しようとしたが、イマイチうまくいかない。
とりあえず、画像の添付ファイルは取得できたのでその方法。
getAttachmentUriの第4,5引数にGmail.AttachmentRendition.SIMPLE, falseと指定すると画像の添付ファイル(jpegやgif)は取得できたが、エクセルファイルやPDFファイルは取得できない。
誰か分かる人教えてください、、、
MessageCursor mc = gmail.getMessageCursorForMessageId(account, messageId);
if(mc.next()){
    ArrayList<Attachment> attInfos = mc.getAttachmentInfos();
    if(!attInfos.isEmpty()){        // 添付があるなら
        for(Iterator<Attachment> ir = attInfos.iterator(); ir.hasNext();){
            Attachment attment = ir.next();
            Log.d(LOGTAG, "attment=" + attment);
            Uri uri = Gmail.getAttachmentUri(account, messageId, attment,Gmail.AttachmentRendition.SIMPLE, false);
            try {
                // 画像を読み込む
                InputStream in = getContentResolver().openInputStream(uri);
                BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();
                Bitmap bm = BitmapFactory.decodeStream(in, null, sBitmapOptions);
                Log.d(LOGTAG, "Bitmap size=" + bm.getWidth() + " x " + bm.getHeight());    // 画像サイズ
                int dispWidth = this.getWindowManager().getDefaultDisplay().getWidth();    // 画面サイズ(幅)
                if(bm.getWidth()> dispWidth){    // 画面幅より大きい時、縮小する
                    bm = Bitmap.createScaledBitmap(bm, dispWidth, bm.getHeight()*dispWidth/bm.getWidth(), false);
                }
                // 画像をファイルに書き込む
                String sdPash = Environment.getExternalStorageDirectory().getPath();    // SDカードのパス
                File sdDir = new File(sdPash + "/android/data/" + getPackageName() + "/cache/");
                sdDir.mkdirs();    //書き込みディレクトリ作成
                String fname = sdDir.getPath() + "/" + mc.getMessageId() + "_" + attment.name;    // 書き込みファイル名
                Log.d(LOGTAG, "fname=" + fname);
                FileOutputStream out = new FileOutputStream(fname);
                bm.compress(CompressFormat.JPEG, 100, out);
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {    // InputStreamエラー処理
                Log.d(LOGTAG, "InputStream ERR! " + e.getMessage());
            } catch (IOException e) {            // FileOutputStreamエラー処理
                Log.d(LOGTAG, "FileOutputStream ERR! " + e.getMessage());
            }
        }
    }
}