我甚至增加用户权限后,越来越mailjava.io.FileNotFoundException

时间:2019-02-02 18:45:40

标签: java android android-intent

我正在创建一个按钮,它将提取数据库,在documents文件夹中创建.xls文件,并将其作为附件发送到电子邮件或whatsapp中。但是,即使向清单文件添加权限后,我也收到mailjava.io.FileNotFoundException错误。我知道我缺少的东西很小,但无法弄清楚。

  1. 我尝试在Internal Cache目录中创建文件,该文件已成功创建,但是无法作为电子邮件或whatsapp的附件获取。
  2. 我在应该保存文件的位置上没有任何问题,因为我作为附件发送后仍要删除它。
  3. 在“电子邮件意图”中,我曾以Uri.parse(“ content://”,file.getAbsolutePath())file.toUri();的身份与Uri一起玩。 Uri uri = Uri.fromFile(file);所有客房都显示这个错误相同的结果。父类具有所有已定义的变量

        imbtnMail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              try {
    
                Cursor cursor = dbManager.fetch(DatabaseHelper.TICKET_TABLE);
    
                String filename = "Report-" + DateUtil.timeMilisToString(System.currentTimeMillis(), "ddMMyy");
                String xlsfile = filename + ".xlx";
                file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), xlsfile);
                Log.i("File written at:",file.getAbsolutePath());
    
                //file path
                WorkbookSettings wbSettings = new WorkbookSettings();
                wbSettings.setLocale(new Locale("en", "EN"));
                WritableWorkbook workbook;
                workbook = Workbook.createWorkbook(file, wbSettings);
                //Excel sheet name. 0 represents first sheet
                WritableSheet sheet = workbook.createSheet("Daily Report", 0);
    
                // column and row
                sheet.addCell(new Label(0, 0, "Pass Report"));
                sheet.addCell(new Label(1, 0, "User"));
    
                if (cursor.moveToFirst()) {
                    do {
                        String Rno = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_RECEIPT));
                        String vno = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_NO));
                        String vtype = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_TYPE));
                        String vin = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_IN_TIME));
                        String vout = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_OUT_TIME));
                        int i = cursor.getPosition() + 1;
                        sheet.addCell(new Label(0, i, Rno));
                        sheet.addCell(new Label(1, i, vno));
                        sheet.addCell(new Label(2, i, vtype));
                        sheet.addCell(new Label(3, i, vin));
                        sheet.addCell(new Label(4, i, vout));
                    } while (cursor.moveToNext());
                }
    
                //closing cursor
                cursor.close();
                workbook.write();
                workbook.close();
    
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("*/*");
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"email@example.com"});
                intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
                intent.putExtra(Intent.EXTRA_TEXT, "body text");
                if (!file.exists() || !file.canRead()) {
                    Toast.makeText(ViewLog.this, "Attachment Error", Toast.LENGTH_SHORT).show();
                    return;
                }
                Uri uri = Uri.fromFile(file);
    
                intent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivityForResult(Intent.createChooser(intent, "Send email..."),ViewLog.REQUEST_WRITE_STORAGE);
            } catch (Exception e) {
                System.out.println("is exception raises during sending mail" + e);
            }
        }
    });
    
  

这是单击此按钮后捕获的错误

     

I / System.out:发送期间是否引发异常   mailjava.io.FileNotFoundException:   /storage/emulated/0/Documents/Report-020219.xlx(无此类文件或   目录)

Manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="mypackagename">
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">
    <activity android:name=".VehicleSetup"></activity>
    <activity android:name=".About" />
    <activity android:name=".Parked" />
    <activity android:name=".AppSetup" />
    <activity android:name=".ViewLog" />
    <activity android:name=".LoginActivity" />
    <activity android:name=".Main" />
    <activity android:name=".Splashscreen">
      <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我希望将此文件写在任何位置,但应作为附件在任何电子邮件客户端或whatsapp中使用

1 个答案:

答案 0 :(得分:0)

我只是发现了造成mailjava.io.FileNotFoundException的错误,因为仅创建了实例,而不是实际文件。

基本上,这有两个错误。

  1. 我在类本身(不是在onClick方法中)声明了File实例(文件)。由于它是在类变量中声明的,因此会抛出FileUriExposedException,该异常确实暴露了catch块中捕获的URI。
  2. 我正在尝试从内部缓存的目录中提取该文件,根据this issue
  3. 在GMail 5.0中是不允许的
  
    

解决方案

  
     
      
  1. 在onClick方法本身中声明文件实例,以免暴露URI。
  2.   
  3. 在ExternalSD中创建文件,因为该文件可以被GMail 5.0全世界访问并接受
  4.   
imbtnMail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Cursor cursor = dbManager.fetch(DatabaseHelper.TICKET_TABLE);
                    File file = new File(Environment.getExternalStorageDirectory(),"Report-"+DateUtil.timeMilisToString(System.currentTimeMillis(),"hh-MM-yy")+".xls");
                    Toast.makeText(getApplicationContext(),"File saved",Toast.LENGTH_LONG).show();

                    WorkbookSettings wbSettings = new WorkbookSettings();
                    wbSettings.setLocale(new Locale("en", "EN"));
                    WritableWorkbook workbook;
                    workbook = Workbook.createWorkbook(file, wbSettings);
                    WritableSheet sheet = workbook.createSheet("Daily Report", 0);

                    // column and row
                    sheet.addCell(new Label(0, 0, "Pass Report"));
                    sheet.addCell(new Label(1, 0, "User"));

                    if (cursor.moveToFirst()) {
                        do {
                            String Rno = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_RECEIPT));
                            String vno = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_NO));
                            String vtype = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_TYPE));
                            String vin = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_IN_TIME));
                            String vout = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_OUT_TIME));
                            int i = cursor.getPosition() + 1;
                            sheet.addCell(new Label(0, i, Rno));
                            sheet.addCell(new Label(1, i, vno));
                            sheet.addCell(new Label(2, i, vtype));
                            sheet.addCell(new Label(3, i, vin));
                            sheet.addCell(new Label(4, i, vout));
                        } while (cursor.moveToNext());
                    }

                    //closing cursor
                    cursor.close();
                    workbook.write();
                    workbook.close();

                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("text/plain");
                    intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email@example.com"});
                    intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
                    intent.putExtra(Intent.EXTRA_TEXT, "body text");
                    if (!file.exists() || !file.canRead()) {
                        Toast.makeText(getApplicationContext(), "Attachment Error", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Uri uri = Uri.fromFile(file);
                    intent.putExtra(Intent.EXTRA_STREAM, uri);
                    startActivity(Intent.createChooser(intent, "Send email..."));
                }
                catch (Exception e){e.printStackTrace();}
            }
        });