2014-05-23 2 views
0

사용자 지정 파일 확장자 (실제로 zip 파일)가있는 전자 메일을 수신하고 있습니다.Android 전자 메일 첨부 파일을 저장하려는 의도

첨부 파일을 예 : K9 이메일 앱을 내 앱의 캐시 디렉토리에 저장하면 zip 파일이 손상됩니다. 이 파일 관리자로 열리지

private void importData(Uri data) { 
    Log.d(TAG, data.toString()); 
     final String scheme = data.getScheme(); 

     if(ContentResolver.SCHEME_CONTENT.equals(scheme)) { 
     try { 
      ContentResolver cr = getApplicationContext().getContentResolver(); 
      InputStream is = cr.openInputStream(data); 
      if(is == null) return; 

      StringBuffer buf = new StringBuffer();    
      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
      String str; 
      if (is!=null) {       
       while ((str = reader.readLine()) != null) { 
        buf.append(str); 
       }    
      }  
      is.close(); 

      File outputDir = getApplicationContext().getCacheDir(); // context being the Activity pointer 
      Log.d(TAG, "outputDir: " + outputDir.getAbsolutePath()); 

      String fileName = "test_seed.zip"; 
      Log.d(TAG, "fileName: " + fileName); 

      File zipFile = new File(outputDir, fileName); 
      FileWriter writer = new FileWriter(zipFile); 
      try { 
       writer.append(buf.toString()); 
       writer.flush(); 
       writer.close(); 
      } catch (IOException e) { 
       Log.d(TAG, "Can't write to " + fileName, e); 
      } finally { 
       Toast.makeText(getApplicationContext(), "Zip key file saved to SDCard.", Toast.LENGTH_LONG).show(); 
      } 
      // perform your data import here… 

     } catch(Exception e) { 
      Log.d("Import", e.getMessage(), e); 
     } 
    } 
} 

열린 문자열이 내 의도 필터입니다

content://com.fsck.k9.attachmentprovider/668da879-32c8-4143-a3ec-e135d901c443/31/VIEW 

입니다 :

<intent-filter> 
<action android:name="android.intent.action.VIEW" /> 
<action android:name="android.intent.action.EDIT" /> 
<category android:name="android.intent.category.DEFAULT" /> 
<data 
    android:mimeType="application/*" 
    android:host="*" 
    android:pathPattern=".*\\.odks" /> 
</intent-filter> 

답변

1

귀하의 BufferedReader.readLine() 문제입니다; 그것은 당신의 파일이 선의 끝을 나타내는 \ n이있는 인간이 읽을 수있는 멋진 텍스트 파일이라고 가정합니다. Zip 파일은 그렇지 않습니다. 대신 InputStream 및 FileOutputStream의 read() 및 write() 메서드를 호출하십시오. 이 라인을 따라

뭔가 :

byte[] buffer = new byte[1024]; 
int n = 0; 
while ((n = infile.read(buffer)) != -1) { 
    outfile.write(buffer, 0, n); 
} 
관련 문제