usman nazir
usman nazir

Reputation: 101

How to parse content data received from intent filters in manifest?

There is an intent filters for kml files that works but I have to receive data in the form of content scheme. The file was picked from File Commander.

data

content://com.mobisystems.fileman.RemoteFiles/ZmlsZTovLy9zdG9yYWdlL2VtdWxhdGVkLzAvTG9jYXRpb24lMjBUcmFja2luZy8yMDE5LTA0LTA0JTIwMTclM0EyNyUzQTU5Lmtt/bA/0

Now the problem is how to read this file? How can I handle this problem? Any help will be appreciated.

So far I tried

var uri = Uri.parse(trackString) 

FileInputStream(contentResolver.openFileDescriptor(uri, "r").fileDescriptor) 

contentResolver.openInputStream(uri) // not working

In manifest.xml

<!--Mime type set -->
            <intent-filter>

                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="content" />

                <!-- Valid mime types -->
                <data android:mimeType="application/vnd.google-earth.kml+xml" />
                <data android:mimeType="application/vnd.google-earth.kmz" />
                <data android:mimeType="application/gpx+xml" />

                <!-- Invalid mime types used by some bad software -->
                <data android:mimeType="application/kml" />
                <data android:mimeType="application/kmz" />
                <data android:mimeType="application/gpx" />

                <data android:mimeType="application/kml+xml" />
                <data android:mimeType="application/kmz+xml" />

                <data android:mimeType="application/vnd.google-earth.kml" />
                <data android:mimeType="application/vnd.google-earth.gpx" />
                <data android:mimeType="application/vnd.google-earth.kmz+xml" />
                <data android:mimeType="application/vnd.google-earth.gpx+xml" />

                <data android:mimeType="text/kml" />
                <data android:mimeType="text/kmz" />
                <data android:mimeType="text/gpx" />

                <data android:mimeType="text/kml+xml" />
                <data android:mimeType="text/kmz+xml" />
                <data android:mimeType="text/gpx+xml" />

                <data android:mimeType="text/xml+kml" />
                <data android:mimeType="text/xml+kmz" />
                <data android:mimeType="text/xml+gpx" />

            </intent-filter>

            <!-- Mime type not set but valid extensions -->
            <intent-filter>

                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />


                <data android:scheme="content" />

                <data android:host="*" />

                <data android:pathPattern="/.*..*..*..*..*\\.gpx" />
                <data android:pathPattern="/.*..*..*..*..*\\.kml" />
                <data android:pathPattern="/.*..*..*..*..*\\.kmz" />
                <data android:pathPattern="/.*..*..*..*\\.gpx" />
                <data android:pathPattern="/.*..*..*..*\\.kml" />
                <data android:pathPattern="/.*..*..*..*\\.kmz" />
                <data android:pathPattern="/.*..*..*\\.gpx" />
                <data android:pathPattern="/.*..*..*\\.kml" />
                <data android:pathPattern="/.*..*..*\\.kmz" />
                <data android:pathPattern="/.*..*\\.gpx" />
                <data android:pathPattern="/.*..*\\.kml" />
                <data android:pathPattern="/.*..*\\.kmz" />
                <data android:pathPattern="/.*\\.gpx" />
                <data android:pathPattern="/.*\\.kml" />
                <data android:pathPattern="/.*\\.kmz" />

            </intent-filter>

            <!-- Invalid mime type but valid extensions -->
            <intent-filter>

                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />


                <data android:scheme="content" />

                <data android:host="*" />
                <data android:mimeType="*/*" />

                <data android:pathPattern="/.*..*..*..*..*\\.gpx" />
                <data android:pathPattern="/.*..*..*..*..*\\.kml" />
                <data android:pathPattern="/.*..*..*..*..*\\.kmz" />
                <data android:pathPattern="/.*..*..*..*\\.gpx" />
                <data android:pathPattern="/.*..*..*..*\\.kml" />
                <data android:pathPattern="/.*..*..*..*\\.kmz" />
                <data android:pathPattern="/.*..*..*\\.gpx" />
                <data android:pathPattern="/.*..*..*\\.kml" />
                <data android:pathPattern="/.*..*..*\\.kmz" />
                <data android:pathPattern="/.*..*\\.gpx" />
                <data android:pathPattern="/.*..*\\.kml" />
                <data android:pathPattern="/.*..*\\.kmz" />
                <data android:pathPattern="/.*\\.gpx" />
                <data android:pathPattern="/.*\\.kml" />
                <data android:pathPattern="/.*\\.kmz" />

            </intent-filter>

Updates

at last it work , I was passing uri.path instead of uri through intent and getting wrong Uri.Parsing, so keep checking that mistake

contentResolver.openInputStream(uri) // works

Upvotes: 2

Views: 685

Answers (2)

Rakhi Dhavale
Rakhi Dhavale

Reputation: 1206

     Cursor returnCursor = context.getContentResolver().query(yourFileUri, null, null, null, null);
     int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
     int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
     returnCursor.moveToFirst();
     String name = (returnCursor.getString(nameIndex));        
     File file = new File(context.getCacheDir(), name);
     try {
        InputStream inputStream = context.getContentResolver().openInputStream(yourFileUri);
        FileOutputStream outputStream = new FileOutputStream(file);
        int read = 0;
        int maxBufferSize = 1 * 1024 * 1024;
        int bytesAvailable = inputStream.available();
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        final byte[] buffers = new byte[bufferSize];
        while ((read = inputStream.read(buffers)) != -1) {
            outputStream.write(buffers, 0, read);
        }
        inputStream.close();
        outputStream.close();
        Log.d(TAG, "Path " + file.getPath());
        Log.d(TAG, "Size " + file.length());
    } catch (Exception e) {
        Log.e("Exception", e.getMessage());
    }

  // To get the mime type of your file 


 public String getMimeType(Uri uri, Context ctx) {
    String mimeType = null;
    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        ContentResolver cr = ctx.getContentResolver();
        mimeType = cr.getType(uri);
    } else {
        String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
                .toString());
        mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                fileExtension.toLowerCase());
    }
    return mimeType;
 }

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006724

Use ContentResolver and:

  • getType() to find out the MIME type of the content (since your <intent-filter> supports lots of types)
  • openInputStream() to read in the content

You get a ContentResolver by calling getContentResolver() on a Context.

Upvotes: 1

Related Questions