0

I am building an application in which I want to send bytearray from one activity to another. In order to do so I have saved the data in the file in byte form 'data1.txt'. At the time of retrieval the app slows down and stops working. This is the code

public void read(String file) {
    String ret = "";
    try {
        InputStream inputStream = openFileInput(file);
        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                ret=ret.concat(receiveString);
            }

            theByteArray = ret.getBytes();
            inputStream.close();
        }
    } catch (FileNotFoundException e) {
        Toast.makeText(getBaseContext(), "File not found: " + e.toString(), Toast.LENGTH_LONG).show();    
    } catch (IOException e) {
        Toast.makeText(getBaseContext(), "Can not read file: " + e.toString(), Toast.LENGTH_LONG).show();    
    }
}
Danielson
  • 2,605
  • 2
  • 28
  • 51
  • Is it textual data you wrote to this file (did you write it using a `Writer`) or is it binary data (written using an `OutputStream`)? – Greg Kopff Jul 29 '15 at 06:59
  • When it stops working you must have a stack trace. Could you copy it here? – StephaneM Jul 29 '15 at 06:59
  • If it is textual data, change `ret` to be a `StringBuilder` and `append` each line to it. – Greg Kopff Jul 29 '15 at 07:00
  • public void save(String file, byte[] data){ try { FileOutputStream fOut = openFileOutput(file,MODE_WORLD_READABLE); fOut.write(data); Toast.makeText(getBaseContext(),"writin done", Toast.LENGTH_SHORT).show(); fOut.close(); } – user3485978 Jul 29 '15 at 07:11
  • I have used fileOutputStrem to save data – user3485978 Jul 29 '15 at 07:11

1 Answers1

1

instead of sharing the byte[] over a file you could send it via an Intent:

byte[] byteArray = new byte[] {};
Intent i = new Intent("name.of.action");
i.putExtra("identifier", byteArray);
startActivity(i);
robocab
  • 777
  • 5
  • 15
  • I have to send multiple data in form of byterarray. The data is too large for the intent – user3485978 Jul 29 '15 at 07:08
  • yes I have. It works fine for one file but I have to send data of four bytearrays. Can you suggest any another method?? – user3485978 Jul 29 '15 at 07:16
  • Another possibility is to share it over a Singleton or the Application instance (which can easily become code smell if not done right) Take a look at this answer: http://stackoverflow.com/a/9385437/2355195 – robocab Jul 29 '15 at 07:18