简体   繁体   English

无法使用android程序从我的手机读取文本文件

[英]unable to read text file from my mobile using android program

It may be a silly question..but I am getting irritated with this.这可能是一个愚蠢的问题......但我对此感到恼火。

The main problem is..I have written code to read a text file from external storage.主要问题是..我已经编写了从外部存储读取文本文件的代码。

In my computer, I installed a software called MEMU like blustaks.在我的电脑上,我安装了一个叫 MEMU 的软件,比如 blustaks。

in menu software, my program is giving good results...but when I am installing that app in my mobile (cool pad note 3 lite) it's showing "unfortunately app is stopped".在菜单软件中,我的程序给出了很好的结果......但是当我在我的手机(cool pad note 3 lite)中安装该应用程序时,它显示“不幸的是应用程序已停止”。

I installed in another mobile "Moto E" but in that mobile also I faced the same problem.我安装在另一部手机“Moto E”上,但在那部手机中我也遇到了同样的问题。

In system software, it is giving outputs..please anyone help me...where is the problem...(the system software is Samsung)在系统软件中,它正在提供输出...请任何人帮助我...问题出在哪里...(系统软件是三星)

my code is////我的代码是////

    signal_number=1;samples=3600;shifting=180;
    File sdCard = Environment.getExternalStorageDirectory();
    String filename ;
    File dir = new File(sdCard.getAbsolutePath());
                    sample_tenSec = FileData.getData(dir, filename, signal_number,samples,shifting);

////////////////////////java code is/////////////////////// ////////////////////////java代码是//////////////////////

    public class FileData {
public static Number[] getData(File dir , String filename, int signalNumber,int samples,int shifting)
{
    File file = null;
    Number[] result = new Number[samples];

    try    //Get the text file
    {
        file = new File(dir+"/"+filename);
        //file=new File("/storage//3483-14D9//ppg.txt");
        if(file.exists())
        {System.out.println("File Exists");}
        else
        {System.out.println("File Does not Exist");}
    }
    catch(Exception e1)
    {
        System.out.println("File not found.");
    }

    // Read from File
    int start = (signalNumber-1)*(int)(shifting) +1;
    int end = (start + (int) (samples)) - 1;
    int i=1,k=0;

    try
    {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null && i<=end)
        {
            if(i>=start)
            {
                try
                { result[k++]= (Double.parseDouble(line.toString())); }
                catch (NumberFormatException discardNonIntegers)
                { discardNonIntegers.printStackTrace(); }
            }
            i++;
        }
        br.close();
    }
    catch (Exception e)
    {
        System.out.println("File not reading problem.");
        //System.out.println(e.getMessage());
    }
    return result;
}

} }

Without seeing the code that you are using, it is hard to pinpoint what the problem is.如果没有看到您正在使用的代码,就很难查明问题所在。 Just reading the paragraph you wrote, I would guess that you are having a problem with permissions.只是阅读您写的段落,我猜您在权限方面遇到了问题。 Remember, you need to not only include static permissions in your manifest, but also runtime permissions as well.请记住,您不仅需要在清单中包含静态权限,还需要包含运行时权限。

Add permissions like these to your manifest if you need to read and write:如果您需要读写,请在清单中添加这些权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Also, you need to implement runtime permissions.此外,您需要实现运行时权限。 All the info you need can be found here:您需要的所有信息都可以在这里找到:

https://developer.android.com/training/permissions/requesting.html https://developer.android.com/training/permissions/requesting.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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