Android - get email attachment name in my application

后端 未结 4 1725
独厮守ぢ
独厮守ぢ 2020-12-18 02:16

I\'m trying to load an email attachment in my application. I can get the content, but I cannot get the file name.

Here\'s how my intent filter looks like:

         


        
相关标签:
4条回答
  • 2020-12-18 02:21

    Here's a "combo solution". Note the final fallback using the mimeType. You will need to define the toExtension() method...

    private static String contentUriFilename( Uri uri ){
        String fileName="";
        if( fileName.isEmpty() )
            fileName = contentUriFilename( uri, android.provider.OpenableColumns.DISPLAY_NAME );
        if( fileName.isEmpty() )
            fileName = contentUriFilename( uri, android.provider.MediaStore.MediaColumns.DISPLAY_NAME );
        if( fileName.isEmpty() )
            fileName = contentUriFilename( uri, "_display_name" );
        if( fileName.isEmpty() ){
            String mimeType;
            try{ mimeType = context_.getContentResolver().getType( uri ); }
            catch( Exception e ){ mimeType=""; }
            fileName = "unknown." + toExtension( mimeType );
        }
        return fileName;
    }
    
    private static String contentUriFilename( Uri uri, String columnName ){
        Cursor cursor = context_.getContentResolver().query( uri,
            new String[]{columnName}, null, null, null);
        cursor.moveToFirst();
        try{
            return cursor.getString(
                        cursor.getColumnIndexOrThrow( columnName ) );
        }
        catch( Exception e ){ return ""; }
    }
    
    0 讨论(0)
  • 2020-12-18 02:24

    The constant MediaStore.MediaColumns.DISPLAY_NAME resolves to the string "_display_name". The array you put in with this string is used to select the columns you want to get with the query. The behavior of null for this parameter might be different for the various phones? Returning all columns (as it should according to the javadoc) by HTC phones and none for others? The javadoc states you should retrieve specific columns for performance reasons. By returning nothing on the null argument, the manufacturers might want to enforce this performance 'optimization'.

    This should therefore work on all phones:

    public static String getContentName (ContentResolver resolver, Uri uri) {
        Cursor cursor = resolver.query(uri, 
           new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
        cursor.moveToFirst();
        int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
        if (nameIndex >= 0) {
            return cursor.getString(nameIndex);
        }
    
        return null;    
    }
    

    I cannot test this, because I only have a HTC phone :).

    0 讨论(0)
  • 2020-12-18 02:25

    Thank you everyone, i solved the problem. But i want to point out an error above. I guess the mobile phone you test is HTC. I use the code above run correctly on my HTC mobile phone, but when i run it on Lenovo mobile phone or M9(A mobile phone from China) i can not get the name of the email file. When id debug the code the variable "nameIndex" return "-1" not the correct number "0", so i can not get the name of the email file. Looking for an afternoon, and finally solve the problem.

    public static String getEmailFileName(ContentResolver resolver, Uri uri){
    Cursor cursor = resolver.query(uri, new String[]{"_display_name"}, null, null, null);
    cursor.moveToFirst();
    int nameIndex = cursor.getColumnIndex("_display_name");
    if (nameIndex >= 0) {
        return cursor.getString(nameIndex);
    } else {
        return null;
    }
    

    }

    0 讨论(0)
  • 2020-12-18 02:32

    I had the same problem to solve today and ended up finding the solution in another post : Android get attached filename from gmail app The main idea is that the URI you get can be used both for retrieving the file content and for querying to get more info. I made a quick utility function to retrieve the name :

    public static String getContentName(ContentResolver resolver, Uri uri){
        Cursor cursor = resolver.query(uri, null, null, null, null);
        cursor.moveToFirst();
        int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
        if (nameIndex >= 0) {
            return cursor.getString(nameIndex);
        } else {
            return null;
        }
    }
    

    You can use it this way in your activity :

    Uri uri = getIntent().getData();
    String name = getContentName(getContentResolver(), uri);
    

    That worked for me (retrieving the name of PDF files).

    0 讨论(0)
提交回复
热议问题