0

Hi I am trying to display the information from a file:

For some reason the first file I used(temp.txt) works and I get this output

Your Body Temperature Readings are: 36.2
32
37.d

my temp.txt file

36.2
32
37.d

however any other file that I send to my phone(via bluetooth) and then try and read with the app presents no output, what am I doing wrong?

For example I sent another file temppp.txt with information below and I got the output:

Your Body Temperature Readings are: 

my temppp.txt

37.06750839
36.89390613
36.88484785
36.81941363
36.81815453

Here is my class which is called on a button click and opens up a new view that has the bodyTempInfoArray textview.

package com.teamfara.circadianrhythmmonitor4;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class bodyTempInfo extends AppCompatActivity {


    ArrayList<Double> currentBodyTemp;

    private void displayBodyTemp(String bodyTemp) {
        TextView textView = (TextView) findViewById(R.id.bodyTempInfoArray);
        textView.setText(bodyTemp);
    }

    public void displayCurrentBodyTempArray(View view) throws FileNotFoundException {

        ArrayList<Double> list = new ArrayList<>();

        File sdcard = Environment.getExternalStorageDirectory();

//Get the text file


//Read text from file

        File file = new File(sdcard, "temppp.txt");

        StringBuilder text = new StringBuilder();

        try {

            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            while ((line = br.readLine()) != null) {
                list.add(Double.parseDouble(line));
                text.append(line);
                text.append('\n');
            }
            br.close();
        }
        catch(IOException e) {

        }

        currentBodyTemp = list;
        displayBodyTemp("Your Body Temperature Readings are: " + text);

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_body_temp_info);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        try {
            displayCurrentBodyTempArray(findViewById(R.id.bodyTempInfoArray));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
  • in your code, the filename is "temppp.txt", but in the example it's named "tempppp.txt". Could it be that you are missing a "p" in the filename? – Andrei Mărcuţ Oct 25 '15 at 20:57
  • that was just a typo in the actual post my bad* it is temppp on my computer and android – Zach Highley-Gergel Oct 25 '15 at 20:59
  • Just to make sure the file is found, update the catch to: (FileNotFoundException e) { throw new Error(e); } . This way you should get a crash if the file cannot be found. Please let me know how it goes. – Andrei Mărcuţ Oct 25 '15 at 21:02
  • 1
    Yep, Adding that crashes the app when I go to read the text file – Zach Highley-Gergel Oct 25 '15 at 21:06
  • Well, this means that the file isn't where your code expects it to be. Can you check it with a file manager on the device (any file manager app would do)? – Andrei Mărcuţ Oct 25 '15 at 21:07
  • I think you may have found the reason its going wrong, the "temp.txt" file(the only one working) is in my "device storage" under "local storage", however the others that don't work(like temppp.txt) are under "documents" under "category", is there anyway to make sure that the file, when sending over bluetooth, goes to device storage? Or a way to read it from the "documents" folder instead of "device storage"? – Zach Highley-Gergel Oct 25 '15 at 21:09
  • You could get started from here: http://stackoverflow.com/questions/10103472/how-to-accept-bluetooth-received-file-in-android-application – Andrei Mărcuţ Oct 25 '15 at 21:15
  • thanks Markus but I'm not worried about recieving or sending the file, just reading it off my device once its there (I am just using bluetooth file transfer on windows to transfer the file from my computer to my phone) – Zach Highley-Gergel Oct 25 '15 at 21:16
  • Or to just use the defaylt bluetooth path, I found this article for you (check Henridv's answer): http://stackoverflow.com/questions/6125993/get-the-bluetooth-storage-folder-on-android-device – Andrei Mărcuţ Oct 25 '15 at 21:17
  • I just added a better solution for you, based on the above post. Please, give it a try – Andrei Mărcuţ Oct 25 '15 at 21:20
  • HI Markus I just added it but a couple things, one "myActivity" I'm assuming is my getBodyInfo class? and i get an error saying non static method getExternalFile cannot be referenced from a static context, also the .txt i'm assuming should be my file? – Zach Highley-Gergel Oct 25 '15 at 21:29
  • I've just updated/adapted the code answer to your quoted class, you shouldn't get the static context error anymore. – Andrei Mărcuţ Oct 25 '15 at 21:33
  • Markus - still no luck it's still not finding the file for some reason, its crashing – Zach Highley-Gergel Oct 25 '15 at 21:41
  • Well, I'm sorry but I'm about to call it a day.. I've continued researching on this topic and it seems it's getting even more complicated. Check out this topic too: http://stackoverflow.com/questions/12087952/bluetooth-folder-different-path-on-different-phones – Andrei Mărcuţ Oct 25 '15 at 21:42
  • No prob thanks for the help I need to somehow edit my download directory to auto go somewhere else – Zach Highley-Gergel Oct 25 '15 at 22:41

0 Answers0