简体   繁体   中英

File not found exception while reading email attachment file from my app in Android

Hi I am trying to read an email attachment from my app.

When I click on the email attachment it opens my app and in that I am trying to read the content of the file using the following code

Intent CallingIntent = getIntent();
Uri data = CallingIntent.getData();     
final String scheme = data.getScheme();

if(ContentResolver.SCHEME_CONTENT.equals(scheme)) 
{    
    ContentResolver cr = getApplicationContext().getContentResolver();
    InputStream is;
    try 
    {
        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);
            }               
        }       
        is.close();
        Toast.makeText(getApplicationContext(), buf, Toast.LENGTH_SHORT).show();                    
    }
    catch (FileNotFoundException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

and when try to open the file using the statement is = cr.openInputStream(data); it gives me an exceptioni FileNotFoundException

Can any suggest how can I accomplish this such in my app I am able to read the content of the attachment without downloading it.

You need to extract real path of file from this URI. This function will return you the path.

// replace this
is = cr.openInputStream(data)


//with
is = cr.openInputStream(getPath(data))


public static String getPath(Uri uri) {

    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = BigNoteActivity.instance.getContentResolver().query(
            uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM