2013-06-30 10 views
5

私は、特別に作成されたバイナリ(.gcsb)ファイルタイプを使用するアプリを持っています。これらのファイルは、sdcardのフォルダに保存されます。私のアプリでGmailの添付ファイルデータにアクセスする方法

現在のところ、ESファイルエクスプローラを使用してオンまたはオフに移動するか、電話メーカーが「USBドライブ」転送ユーティリティのように動作します。 Clunky。私がしたいことは、ファイルを電話にメールで送信し、gmail内の添付ファイルとしてファイルを開くことができるようにすることです。これにより、アプリケーションが起動し、適切なSDカードフォルダに保存されます。

Gmail(特にOpen custom Gmail attachment in my Android app)内の「プレビュー」をクリックするとアプリが起動するようになっていますが、ファイルデータへのアクセス方法はわかりません!私はそれがIntent.get ... Extra()関数のどれかと同じでなければならないと思うが、どちらを使うべきか?

答えて

20

これを行う方法がわかりました。うまくいけば、これは他の誰かを助けるかもしれない。党の鉱山、一部は他の柱から。 .gcsb添付ファイルを処理することを目指しています。

意図-フィルタは

<intent-filter> 
    <action android:name="android.intent.action.VIEW" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
    <data android:mimeType="application/octet-stream" /> 
</intent-filter> 

と活動のコードのonCreate()で/ onRestart()標準のAndroidのGmailとメールアプリケーションで動作しているように見えます

Intent intent = getIntent(); 
InputStream is = null; 
FileOutputStream os = null; 
String fullPath = null; 

try { 
    String action = intent.getAction(); 
    if (!Intent.ACTION_VIEW.equals(action)) { 
     return; 
    } 

    Uri uri = intent.getData(); 
    String scheme = uri.getScheme(); 
    String name = null; 

    if (scheme.equals("file")) { 
     List<String> pathSegments = uri.getPathSegments(); 
     if (pathSegments.size() > 0) { 
      name = pathSegments.get(pathSegments.size() - 1); 
     } 
    } else if (scheme.equals("content")) { 
     Cursor cursor = getContentResolver().query(uri, new String[] { 
      MediaStore.MediaColumns.DISPLAY_NAME 
     }, null, null, null); 
     cursor.moveToFirst(); 
     int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME); 
     if (nameIndex >= 0) { 
      name = cursor.getString(nameIndex); 
     } 
    } else { 
     return; 
    } 

    if (name == null) { 
     return; 
    } 

    int n = name.lastIndexOf("."); 
    String fileName, fileExt; 

    if (n == -1) { 
     return; 
    } else { 
     fileName = name.substring(0, n); 
     fileExt = name.substring(n); 
     if (!fileExt.equals(".gcsb")) { 
      return; 
     } 
    } 

    fullPath = ""/* create full path to where the file is to go, including name/ext */; 

    is = getContentResolver().openInputStream(uri); 
    os = new FileOutputStream(fullPath); 

    byte[] buffer = new byte[4096]; 
    int count; 
    while ((count = is.read(buffer)) > 0) { 
     os.write(buffer, 0, count); 
    } 
    os.close(); 
    is.close(); 
} catch (Exception e) { 
    if (is != null) { 
     try { 
      is.close(); 
     } catch (Exception e1) { 
     } 
    } 
    if (os != null) { 
     try { 
      os.close(); 
     } catch (Exception e1) { 
     } 
    } 
    if (fullPath != null) { 
     File f = new File(fullPath); 
     f.delete(); 
    } 
} 

です。 gmailで 'ダウンロード'(スキームファイル)または 'プレビュー'(スキームコンテンツ)が押されたかどうかによって、ファイル名は2つの異なる方法で取得されます。

アクティビティが単一インスタンスに設定されていないことが非常に重要であることに注意してください。

+0

のために動作しますので、ありがとうございました!私は何時間もこのようなことに執着してきました。 – sigmabeta

+0

ちなみに、上記のコードを動作させるために必要な権限があるかどうか知っていますか? – sigmabeta

+0

ありがとう、まっすぐに働いた! – mifthi

2

nwm01223 '答えが正しい! 小さな付録: 彼の答えはACTION_VIEWでのみ有効です。あなたは

Uri uri = null; 
if(Intent.ACTION_VIEW.equals(action)){ 
    uri = intent.getData(); 
} else if(Intent.ACTION_SEND.equals(action)){ 
    uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); 
} 

を追加する場合 それはまた、ACTION_SEND

関連する問題