0

I'm working on a method that gets the file path of a file that i'm opening from my email. But the path stays empty.

Code that should do the trick :

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");
   }
}

My intent that opens the mail.

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

I hope you guys will find the trick. I've search for a while now, but cant seem to find similar problems/solutions.

EDIT : The files i'm talking about is a XML (.dcs) file, i just need the location to open and parse it with my XML Parser.

Jordi Sipkens
  • 595
  • 1
  • 9
  • 25
  • http://stackoverflow.com/questions/17388756/how-to-access-gmail-attachment-data-in-my-app – Pedro Oliveira Sep 12 '14 at 11:35
  • That link is about media files, do you have any input on xml files? So that when i get the file URI i can start my XML Parser ? Sorry should have cleared that up from the beginning – Jordi Sipkens Sep 12 '14 at 12:12
  • I have your answer, if it works with attachments... Basically you want the real path from uri it should work, I'll post it. – M. Reza Nasirloo Sep 12 '14 at 13:43
  • I got a bit further, but now when i open the file i get a gmail link instead of a file link. This is the error im getting when trying to open the file. 09-12 16:05:26.867: W/System.err(11872): java.io.FileNotFoundException: /gillzerror@gmail.com/messages/423/attachments/0.1/BEST/false: open failed: ENOENT (No such file or directory) – Jordi Sipkens Sep 12 '14 at 14:14
  • @Pedram , could you please post the possible solution? – Jordi Sipkens Sep 12 '14 at 15:02
  • Tell first which absolute path you retrieve from the data from the intent. Which file are you opening that you get a link? Is that link mentioned in the file content? Don't try to open a file. Just first display, log or toast the filename/path that you retrieve. – greenapps Sep 12 '14 at 15:43
  • Dit you try intent.getDataString(); ? – greenapps Sep 12 '14 at 15:58
  • 'a method that gets the file path of a file that i'm opening from my email.'. Opening...? That you pick from your email app. Please tell what the user has to do in the email app to pick a certain attachment. As you know attachments are no files. But attachments can be saved to file. So please tell what all is happening. – greenapps Sep 12 '14 at 16:23

2 Answers2

0

A few month ago I asked a question like this on SO, then I found a project on github that solved my problem. Here is the question link and the classes you need: FileUtils and LocalStorageProvider

Usage:

Call this and pass the Context and your uri, you'll get the file path:

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

If you've got no luck, just try to remove these asterisks:

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

It's a reach class indeed, so try to dig in a little bit, you'll find good things.

Community
  • 1
  • 1
M. Reza Nasirloo
  • 16,434
  • 2
  • 29
  • 41
0

I've found my own solution, alot easier than the above in my opinion. But thanks anyway @Pedram

In 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();
            }
        }

Then made a method in the same class :

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){

            }
          }
    }

Then the final method the parser :

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();
            }
    }

Sources : 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/

I've adapted the methods to my own xml file i needed to readout. Hope this will help anyone else in the future!

Note: the importData gave the file as the content example : (picture them with bracket <>)

xml

config

settings

etc...

So to fix this i just used a inputstream with bytearrayinputstream.

Jordi Sipkens
  • 595
  • 1
  • 9
  • 25