打开并保存文件路径android

时间:2014-09-12 11:26:08

标签: android android-intent uri

我正在研究一种方法,该方法获取我从电子邮件中打开的文件的文件路径。但路径保持空白。

应该做的诀窍的代码:

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 Parser解析它。

2 个答案:

答案 0 :(得分:0)

几个月前我在SO上问过这样的问题,然后我在github上发现了一个解决了我的问题的项目。以下是question link以及您需要的课程:FileUtilsLocalStorageProvider

<强>用法:

拨打此电话并传递Contexturi,您将获得文件路径:

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/*";

确实是一个伸手可及的课程,所以尽量挖掘一下,你会发现好东西。

答案 1 :(得分:0)

我找到了自己的解决方案,在我看来比上面的方法更容易。但无论如何,谢谢@Pedram

on 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将文件作为内容示例:(用括号&lt;&gt;画出来)

XML

配置

设置

等...

所以为了解决这个问题,我只使用了带有bytearrayinputstream的输入流。