Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建文件夹、创建文件、打印日志、发送电子邮件_Java_Android_Eclipse - Fatal编程技术网

Java 创建文件夹、创建文件、打印日志、发送电子邮件

Java 创建文件夹、创建文件、打印日志、发送电子邮件,java,android,eclipse,Java,Android,Eclipse,我对Android开发还是很陌生,所以如果我的问题看起来很愚蠢,我会提前道歉 在我的应用程序中,我有一个按钮。单击按钮时,它会尝试查看应用程序在内部存储器上是否有自己的文件夹,如果没有,它会创建一个文件夹,然后它会创建一个名为output.txt的文件,然后它会将系统信息写入output.txt,然后它会尝试将logcat中包含“SIP_消息”的所有行写入output.txt,然后,它会将默认的电子邮件地址发送给用户 编辑 经过几天的努力,我总算把一切都安排好了。请详细阅读下面的答案。哎呀!说

我对Android开发还是很陌生,所以如果我的问题看起来很愚蠢,我会提前道歉

在我的应用程序中,我有一个按钮。单击按钮时,它会尝试查看应用程序在内部存储器上是否有自己的文件夹,如果没有,它会创建一个文件夹,然后它会创建一个名为output.txt的文件,然后它会将系统信息写入output.txt,然后它会尝试将logcat中包含“SIP_消息”的所有行写入output.txt,然后,它会将默认的电子邮件地址发送给用户


编辑 经过几天的努力,我总算把一切都安排好了。请详细阅读下面的答案。

哎呀!说话太早了,你的意思是内部的。

你为什么不尝试直接将文件写入应用程序目录,而不是创建文件夹呢。首先检查它是否有效

您可以这样做:

这就是您要找的:

您还需要添加此权限:



您的清单中是否有android.permission.READ_LOGS权限?

对于电子邮件部分,您是否打算自动发送

如果是这样的话,我认为在后台做起来并不容易。最好是调用Gmail应用程序,并将output.txt作为附件附加。随后,您需要手动发送它

否则编码可能很长,请参见此处:


或者另一种方式是通过短信发送,这可以在后台完成。

经过几天的研究和无数次的猜测和检查,我终于明白了一切。我想用这段时间来解释所有的事情,以防有人遇到这个问题,并且遇到和我一样的问题。希望你正在寻找的一切都在这里,我给出了一个比你(和我)之前访问过的其他100个网站更好的解释

第一个主题是内部存储和外部存储之间的区别(不是SD卡和非SD卡之间的区别)

除了您的应用程序之外,没有人可以看到或访问内部存储。如果在内部存储器上创建文件或文件夹,则无法使用文件浏览器(除非您的根目录)或计算机查看您创建的内容,从应用程序外部完全无法访问该文件或文件夹

从技术上讲,文档/下载/音乐/铃声等公共文件夹位于外部存储器上。您需要对其进行写入和读取的权限。这就是我感到困惑的地方。我认为只有SD卡才算是外部存储,外部存储是可以从计算机或文件浏览器手动获取的东西,无论它是否在SD卡上

要在内部或外部存储器上创建文件,不需要使用mkDir()。任何一个说你愿意的人,都会使事情变得过于复杂。实际上,您可以通过以下代码在系统的任何位置创建任何文本文件:

PrintWriter osw = new PrintWriter(Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).toString() + "/output.txt");
这将在下载目录中创建一个文本文件,不管它是否首先存在。您还可以使用getDataDirectory()或任何您想要创建文件的地方

下一个Logcat,就像其他人指出的,我试图在Logcat被创建时阅读它。logcat没有尽头,所以实际上,我的应用程序挂起了,因为它一直在寻找更多的东西来编写。解决这个问题的一个简单方法是使用logcat的-d特性。它的作用是,它只需要把所有的东西都带到输入-d的地方(这正是我想要的),然后它停止,然后你可以把它放到一个缓冲区中,不挂起就可以得到输出

最后,将文件附加到电子邮件意图。这是一个棘手的问题,因为有几个不同的领域最终给我带来了问题。简言之,如果您收到错误,“无法显示附件”,它意味着两件事之一-1)您试图从内存附加文件(请记住,不允许其他程序访问内存,甚至gmail)或2。)您没有使用getAbsolutePath()。我发现很多人说你不能使用uri.parse()附加文件,而你必须使用uri.fromFile(),这是错误的,附件我向你展示了如何附加文件而不出现错误

我希望这段代码对您有所帮助,并且我希望您不要花费我所花时间的十分之一来尝试解决这些问题

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Calendar;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button mailb = (Button)findViewById(R.id.bmail);
    final TextView confirmation = (TextView)findViewById(R.id.Confirmation);
    mailb.setOnClickListener(new View.OnClickListener() {   
    @Override

    public void onClick(View v) {
        try {     
            PrintWriter osw = new PrintWriter(Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).toString() + "/output.txt"); //This creates a file in my public download directory
               osw.println("Output Log: Report Tool");
               osw.println("Date: " + java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime())); 
               osw.println("------------------------------------");
               osw.println("Manufacturer: " + android.os.Build.MANUFACTURER);
               osw.println("Model: " + android.os.Build.MODEL);
               osw.println("Serial: " + android.os.Build.SERIAL);
               osw.println("BootLoader: " + android.os.Build.BOOTLOADER);
               osw.println("Build ID: " + android.os.Build.FINGERPRINT);
               osw.println("------------------------------------");
              try { 
                  Process p = Runtime.getRuntime().exec("logcat -d -v long"); //This gets the dump of everything up to the button press
                  BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                  String line = null;
                  while ((line = reader.readLine()) != null) {
                    if(line.toString().contains("SIP_MESSAGE")){ //This parses out everything but SIP Messages
                    osw.println(line); }}}
              catch (IOException e1) {confirmation.setText(e1.getMessage()); }

               osw.flush();
               osw.close();

        } catch(Exception e){ confirmation.setText(e.getMessage()); }

        String attach = Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).getAbsolutePath() + "/output.txt"; //This is where you need to use the absolute path!!
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"MyEmail@Email.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, "Error Report.");
        i.putExtra(Intent.EXTRA_TEXT   , "Please see the attached file...");
        i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + attach)); //This is where you attach the file 
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));}
        catch (android.content.ActivityNotFoundException ex) {
            confirmation.setText("There is no Email Client installed on this device.");}                
    }
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}    
最后,我使用的权限是READ_LOGS,WRITE_EXTERNAL,READ_EXTERNAL


我希望你过得愉快,祝你好运。

你现在正在阅读《航海日志》。。。那里有多少行?从logcat读取的行数可能是那里的瓶颈,因此,当logcat不断收到垃圾邮件时,不仅是你的应用程序,而且是整个Android系统,它似乎“挂起”了!嗯,看来你是对的,这就是应用程序挂起的地方。。。我知道logcat是一个只能达到一定大小的内存缓冲区,如果你愿意的话,可以使用fifo。从缓冲区开始到单击按钮,是否还有保存日志的方法?我无法使用get External Storage。这是必须的internal@Nefarii,我知道我的缺点。我已经更新了答案。但是,我真的很喜欢你的答案。它不是自动发送的,它只是通过调用Intent来调用GMail应用程序。这很好。是的,我知道,它似乎一直在试图从日志中读取,而不仅仅是我希望得到好答案的快照。我不知道gmail无法访问您的内部存储,即使您以绝对路径通过。你确定吗?关于logcat的建议也不错
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
PrintWriter osw = new PrintWriter(Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).toString() + "/output.txt");
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Calendar;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button mailb = (Button)findViewById(R.id.bmail);
    final TextView confirmation = (TextView)findViewById(R.id.Confirmation);
    mailb.setOnClickListener(new View.OnClickListener() {   
    @Override

    public void onClick(View v) {
        try {     
            PrintWriter osw = new PrintWriter(Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).toString() + "/output.txt"); //This creates a file in my public download directory
               osw.println("Output Log: Report Tool");
               osw.println("Date: " + java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime())); 
               osw.println("------------------------------------");
               osw.println("Manufacturer: " + android.os.Build.MANUFACTURER);
               osw.println("Model: " + android.os.Build.MODEL);
               osw.println("Serial: " + android.os.Build.SERIAL);
               osw.println("BootLoader: " + android.os.Build.BOOTLOADER);
               osw.println("Build ID: " + android.os.Build.FINGERPRINT);
               osw.println("------------------------------------");
              try { 
                  Process p = Runtime.getRuntime().exec("logcat -d -v long"); //This gets the dump of everything up to the button press
                  BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                  String line = null;
                  while ((line = reader.readLine()) != null) {
                    if(line.toString().contains("SIP_MESSAGE")){ //This parses out everything but SIP Messages
                    osw.println(line); }}}
              catch (IOException e1) {confirmation.setText(e1.getMessage()); }

               osw.flush();
               osw.close();

        } catch(Exception e){ confirmation.setText(e.getMessage()); }

        String attach = Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).getAbsolutePath() + "/output.txt"; //This is where you need to use the absolute path!!
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"MyEmail@Email.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, "Error Report.");
        i.putExtra(Intent.EXTRA_TEXT   , "Please see the attached file...");
        i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + attach)); //This is where you attach the file 
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));}
        catch (android.content.ActivityNotFoundException ex) {
            confirmation.setText("There is no Email Client installed on this device.");}                
    }
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}