相关疑难解决方法(0)

在Android中创建临时文件

在Android中创建临时文件的最佳方法是什么?

可以使用File.createTempFile吗?文档对此非常模糊.

特别是,不清楚创建的临时文件何时File.createTempFile被删除.

file-io android temporary-files

193
推荐指数
4
解决办法
15万
查看次数

如何在Android中发送包含文件附件的电子邮件

我想在我的邮件中附上.vcf文件并通过邮件发送.但邮件收到的地址没有附件.我使用下面的代码,但代码为此,我不知道我错在哪里.

try {      
  String filelocation="/mnt/sdcard/contacts_sid.vcf";      
  Intent intent = new Intent(Intent.ACTION_SENDTO);    
  intent.setType("text/plain");      
  intent.putExtra(Intent.EXTRA_SUBJECT, "");      
  intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation));      
  intent.putExtra(Intent.EXTRA_TEXT, message);         
  intent.setData(Uri.parse("mailto:"));         
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

  activity.startActivity(intent);
  activity.finish();
  } catch(Exception e)  {
     System.out.println("is exception raises during sending mail"+e);
}
Run Code Online (Sandbox Code Playgroud)

email android vcf email-attachments

47
推荐指数
2
解决办法
8万
查看次数

FileProvider在GetUriForFile上抛出异常

在FileProviders上的android开发人员参考上遵循了示例代码,但它不起作用.

files/exports/在我的Manifest中的文件提供程序定义中设置了路径,并且引用的文件确实存在于该路径中.

在我的清单中:

<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.company.app.fileprovider" android:exported="false" android:grantUriPermissions="true">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)

并在 xml/file_paths.xml

<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="exports" path="exports/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用以下代码获取content-uri:

Java.IO.File exportFile = 
    new Java.IO.File ("/data/data/com.company.app/files/exports/file.pdf"); 
Android.Net.Uri exportUri = 
    Android.Support.V4.Content.FileProvider.GetUriForFile (this, "com.company.app.fileprovider", exportFile);
Run Code Online (Sandbox Code Playgroud)

但是我收到此错误:

Exception of type 'Java.Lang.NullPointerException' was thrown.
  at Android.Runtime.JNIEnv.CallStaticObjectMethod (IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00064] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1160 
  at Android.Support.V4.Content.FileProvider.GetUriForFile (Android.Content.Context context, System.String authority, Java.IO.File file) [0x00000] in <filename unknown>:0 
  at com.company.App.ImageAndSubtitle.cloudStorageDidDownloadFile (System.Object sender, com.company.App.CloudStorageEventArgs …
Run Code Online (Sandbox Code Playgroud)

android android-fileprovider

24
推荐指数
2
解决办法
2万
查看次数

Android将文件附加到GMAIL - 无法附加空文件

我有一个程序,总是将相同的文件附加到GMAIL(Compose> Attach File> Open From>"MyProgram").它总是会选择相同的文件.

它做的是:

String path = Environment.getExternalStorageDirectory() + "/file.3gp";
File f = new File(path);
Uri data = Uri.fromFile(f);
Intent i = new Intent();
i.setData(data);
setResult(Activity.RESULT_OK, i);
finish();
Run Code Online (Sandbox Code Playgroud)

这个工作正常,直到Android 6.0.现在,我在尝试使用它时收到以下错误:

无法附加空文件

Astro文件共享给了我同样的错误(可以是旧版本).

但是,我安装了ES文件资源管理器,当我执行相同的例程并选择文件时,我收到一个Dialog,其中说:

拿起文件为

  • 普通Android方式(适用于彩信,Gmail,...)
  • 文件方式(如果失败,请尝试此操作)

"文件方式"将失败,因为我的程序."普通的Android方式"将正常工作.

有没有人知道它的作用,所以我可以复制?

提前致谢!

OBS:已经尝试了很多次putExtra(STREAM,路径),但没有成功.

android attachment android-intent

20
推荐指数
2
解决办法
2万
查看次数

在Android上以编程方式发送带附件的电子邮件

我希望实现一个按钮,按下它将打开带有附件文件的默认电子邮件客户端.

我正在关注这个,但是我在startActivity上收到一条错误消息,说它正在期待一个活动参数,而我正在给它一个意图.我使用的是API 21和Android Studio 1.1.0,所以它可能与链接中提供的答案中的注释有关?

这是我作为Android开发人员的第四天,很抱歉,如果我遗漏了一些非常基本的东西.

这是我的代码:

    public void sendFileToEmail(File f){

    String subject = "Lap times";
    ArrayList<Uri> attachments = new ArrayList<Uri>();
    attachments.add(Uri.fromFile(f));
    Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
    intent.setClassName("com.android.email", "com.android.mail.compose.ComposeActivity");

    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

email android email-attachments android-intent

16
推荐指数
2
解决办法
3万
查看次数

Gmail"无法附加空文件"

我的应用程序使用iText库创建PDF(带有填写表格的模板PDF),然后我想将其附加到要发送的电子邮件中.当我尝试附加文件时,我Can't attach empty file在Gmail应用中收到了错误消息.还试过HTC电子邮件应用程序和Touchdown电子邮件应用程序 - 两者都没有附加任何内容并且没有错误.

创建PDF:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        AcroFields form = stamper.getAcroFields();

// Fill all PDF forms here (theres quite a few)

        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
}
Run Code Online (Sandbox Code Playgroud)

试图附加文件和发送电子邮件:

public void sendMail(){
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent .setType("*/*");
    emailIntent .putExtra(Intent.EXTRA_EMAIL, "blah@gmail.com");
    emailIntent .putExtra(Intent.EXTRA_STREAM, Uri.parse(dest));
    emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
    startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
Run Code Online (Sandbox Code Playgroud)

相关dest文件的路径和文件名在哪里.我知道路径和文件名dest是正常的,因为这是用于保存文件的var(我可以在设备和PC上查看).

Gmail logcat: …

android email-attachments

15
推荐指数
1
解决办法
8521
查看次数

Android - 无法在电子邮件中附加文件

默认情况下,保存到内部存储的文件对应用程序是私有的,而其他应用程序无法访问它们(用户也无法访问).

我能够在DDMS中看到文件"/ data/data/package_name/files/in file explore,但是当我在电子邮件中使用imageUri附加上述文件URI时,我看到附件是0kb.我使用了Android的默认电子邮件API.

任何人都可以建议我,如何在应用程序私有的电子邮件中附加文件?

虽然我能成功地将文件保存在SD卡中并从SD卡附加文件,但这很好用.

但是,如果SD卡不可用并将文件保存到内部存储器,那么我如何将它们附加到电子邮件中.

String FILENAME = "hello_file.txt";
String string = "hello world!";FileOutputStream fos = openFileOutput(FILENAME,     Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

File imageFile = getFileStreamPath(FILENAME );
Uri imageUri = Uri.fromFile(imageFile);

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("*/*");
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,imageUri);

this.startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."),SUB_ACTIVITY);
Run Code Online (Sandbox Code Playgroud)

email android attachment

12
推荐指数
3
解决办法
2万
查看次数

尝试将文件附加到电子邮件意图的"附件权限被拒绝"(在Gmail 5.0上)

这个问题之前已经发布过,但没有明确或接受的答案,所提供的所有"工作"的解决方案都不适合我.请参阅此处:Gmail 5.0应用程序在收到ACTION_SEND意图时失败并显示"附件权限被拒绝"

我有一个应用程序,它在文本文件中构建数据,需要在电子邮件中发送文本文件,自动附加它.我已经尝试了许多方法来实现这一点,它显然适用于Gmail 4.9及更低版本,但5.0具有一些新的权限功能,使其无法按照我的意愿行事.

    Intent i = new Intent(Intent.ACTION_SEND);

    String to = emailRecipient.getText().toString();

    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
    i.putExtra(Intent.EXTRA_SUBJECT, "Pebble Accelerometer Data");
    i.putExtra(Intent.EXTRA_TEXT, "Attached are files containing accelerometer data captured by SmokeBeat Pebble app.");
    String[] dataPieces = fileManager.getListOfData(getApplicationContext());
    for(int i2 = 0; i2 < dataPieces.length; i2++){
        i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(getApplicationContext().getFilesDir() + File.separator + dataPieces[i2])));
    }
    i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(getApplicationContext().getFilesDir() + File.separator + fileManager.getCurrentFileName(getApplicationContext()))));
    Log.e("file loc", getApplicationContext().getFilesDir() + File.separator + fileManager.getCurrentFileName(getApplicationContext()));
    try {
        startActivity(Intent.createChooser(i, "Send Email"));
    } catch (android.content.ActivityNotFoundException ex) …
Run Code Online (Sandbox Code Playgroud)

permissions gmail android android-intent

9
推荐指数
1
解决办法
9626
查看次数