打开并保存文件路径android

问题描述:

我正在研究一种获取我从我的电子邮件打开的文件的文件路径的方法。但道路仍然空无一人。打开并保存文件路径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

我发现我自己的解决方案,比我认为的更容易。但还是谢谢你@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输入流。

几个月前,我在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/*"; 

这确实是一个到达类,所以试着挖一点点,你会发现很好的东西。