2014-09-12 171 views
0

我正在研究一種獲取我從我的電子郵件打開的文件的文件路徑的方法。但道路仍然空無一人。打開並保存文件路徑android

應該做的伎倆

代碼:

protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 
    if (requestCode == PICK_REQUEST_CODE) 
    { 
    if (resultCode == RESULT_OK) 
    { 
     Uri uri = intent.getData(); 
     String type = intent.getType(); 
     Log.i("Docspro File Opening","Pick completed: "+ uri + " "+type); 
     if (uri != null) 
     { 
     path = uri.toString(); 
     if (path.startsWith("file://")) 
     { 
      // Selected file/directory path is below 
      path = (new File(URI.create(path))).getAbsolutePath(); 
      ParseXML(path); 
     } 

     } 
    } 
    else Log.i("Docspro File Opening","Back from pick with cancel status"); 
    } 
} 

我打開的郵件意圖。

public void openEmail(View v) 
{ 
    Intent emailItent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm"); 
    startActivityForResult(emailItent, 1); 
} 

我希望你們能找到把戲。我現在搜索了一段時間,但似乎找不到類似的問題/解決方案。

編輯:我所說的文件是一個XML(.dcs)文件,我只需要打開位置並使用我的XML解析器解析它。

+0

http://stackoverflow.com/questions/17388756/how-to-access-gmail-attachment-data-in-my-app – 2014-09-12 11:35:06

+0

該鏈接是關於媒體文件,你有任何輸入的XML文件?所以當我得到文件的URI我可以啓動我的XML解析器?對不起,應該從頭開始清除 – 2014-09-12 12:12:25

+0

我有你的答案,如果它與附件一起工作...基本上你想從uri的真正路徑它應該工作,我會發布它。 – Pedram 2014-09-12 13:43:32

回答

0

我發現我自己的解決方案,比我認爲的更容易。但還是謝謝你@Pedram

在的onCreate:

Uri data = getIntent().getData(); 
     if(data!=null) 
     { 
      getIntent().setData(null); 
      try { 
       importData(data); 
      } catch (Exception e) { 
       // warn user about bad data here 
       Log.d("Docspro", "Opening file failed"); 
       e.printStackTrace(); 
      } 
     } 

然後做在同一個類中的方法:

private void importData(Uri data) { 
      final String scheme = data.getScheme(); 

      if(ContentResolver.SCHEME_CONTENT.equals(scheme)) { 
      try { 
       ContentResolver cr = this.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 + "\n"); 
       }    
       }  
       is.close(); 

       // perform your data import here… 
       ParseXML(buf.toString()); 
      } 
      catch(IOException e){ 

      } 
      } 
    } 

然後最終方法解析器:

public void ParseXML(String file) 
    { 
     try { 
      InputStream is = new ByteArrayInputStream(file.getBytes("UTF-8")); 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(is); 

      //optional, but recommended 
      //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work 
      doc.getDocumentElement().normalize(); 

      Log.i("Testing", "Root element :" + doc.getDocumentElement().getNodeName()); 

      NodeList nList = doc.getElementsByTagName("Settings"); 

      Log.i("Testing","----------------------------"); 

      for (int temp = 0; temp < nList.getLength(); temp++) { 

       Node nNode = nList.item(temp); 

       Log.d("Testing", "\nCurrent Element :" + nNode.getNodeName()); 

       if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

        Element eElement = (Element) nNode; 
        DCSEmail = eElement.getElementsByTagName("User").item(0).getTextContent(); 

        if(DCSEmail.equals(Settings.getEmail())) 
        { 
         Settings.setAccount(true); 
         Log.d("waarde account", "Waarde : " + Settings.getAccount() + " & Waarde DCSEMAIL : " + DCSEmail); 
        } 
        else 
        { 
         Settings.setAccount(false); 
        } 
       } 
      } 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } 
    } 

來源:http://richardleggett.co.uk/blog/2013/01/26/registering_for_file_types_in_android/ http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

我調整了方法到我自己的xml文件,我需要讀出。希望這將有助於未來的其他人!

注:的IMPORTDATA給文件作爲內容的示例:

XML

配置

設置

等(帶支架<>圖片它們).. 。

所以要解決這個問題,我只是用bytearrayinputstream輸入流。

0

幾個月前,我在SO上問過這樣一個問題,然後我在github上找到了一個解決我的問題的項目。這裏是question link,你需要的類:FileUtilsLocalStorageProvider

用法:

調用此並通過Context和你uri,你會得到的文件路徑:

String filePath = FilesUtils.getPath(this, uri); 

如果你沒有運氣,只是嘗試刪除這些星號:

public static final String MIME_TYPE_AUDIO = "audio/*"; 
public static final String MIME_TYPE_TEXT = "text/*"; 
public static final String MIME_TYPE_IMAGE = "image/*"; 
public static final String MIME_TYPE_VIDEO = "video/*"; 
public static final String MIME_TYPE_APP = "application/*"; 

這確實是一個到達類,所以試着挖一點點,你會發現很好的東西。