Reading a .txt file from the shared file associated with a remote Android studio camera

I am trying to read a .txt file from dropbox with a shared public link. What I want to do is read this .txt and display all the data inside this file in a list in android.

http://txt.do/5zflt (I do not have delete access on my current computer, so I want to use this link as an example)

The file is called PersonStatus, which will contain text that will contain lines:

Online Offline Active Holidays …. …. … … … … 

basically what i want to do is use a generic delete link to read this text and display it on my list on Android, but I'm not sure how I can do this. I searched online for tutorials and tutorials, but as a newbie to android, I couldn't find something very useful;

For example, I found this link: I read the file from Dropbox , where the OP asked a similar question, but did not provide me with enough code to understand how I can approach this. In addition, in my research, I found that Dropbox has an Android Sync API: https://www.dropbox.com/developers-v1/sync/start/android , but as a newbie in programming, I'm not quite sure how to do it by implementing and making it work.

I would really appreciate if anyone could help. Thank you in advance. If my question is not clear, let me know and I will try to explain it better.

+5
source share
2 answers

I put here an example of the implementation of the project implementation script on GitHub (I also posted a public file with the structure that you reported here on Dropbox). Inside the project you will find the following main components:

  • MainActivity . It includes a RecyclerView , which will be populated with lines of file content. To get the contents of the file, the activity depends on the saved fragment, which allows you to save the download task in case of screen rotation (see here for detailed information on configuration changes). The file is loaded automatically as soon as the activity is created, but you can force it to reload using the SYNC button on the action bar.
  • DownloadFragment . This is the saved snippet that wraps AsyncTask used to download the file. It provides a Callback implemented by MainActivity to handle certain events that occur during loading (for example, onPrepare, onProgress, onDownloadCompleted, onDownloadFailed). You can use them, for example, to display a progress bar or other user feedback.
  • FileContentAdapter . This is the adapter used to display the contents of a file inside RecyclerView .

Some limitations

  • This app is not focused on Dropbox. In case the file is open on the Internet, you can download it regardless of who places it. If your goal is that the operation is automatically synchronized with the file in Dropbox, it is probably best to use the Dropbox SDK, in particular if you plan to access files that are private in Dropbox.
  • AsyncTask should be improved, for example, by implementing WakeLock .
+5
source

In my applications, I use this code to retrieve the contents of a shared Dropbox file. I call this code inside AsyncTask.

Edited . Here is an example

 public class DropboxSampleActivity extends Activity { private ListView listViewDropbox; private ArrayAdapter<String> adapter = null; private static String URL_FILE_DROPBOX = "https://www.dropbox.com/s/xxxxxxxxxxxx/xxxxxxxxxxxx?dl=1"; private ArrayList<String> listElementItem; @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_dropbox_list); super.onCreate(savedInstanceState); listViewDropbox = (ListView) findViewById(R.id.listViewDropbox); DropboxItemAsyncTask dropboxItemAsyncTask = new DropboxItemAsyncTask(); dropboxItemAsyncTask.execute(); } class DropboxItemAsyncTask extends AsyncTask { protected Integer doInBackground(Object[] params) { try { listElementItem = new ArrayList<>(); URLConnection conn = new URL(URL_FILE_DROPBOX).openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader( is, "UTF-8"), 8); String line = null; while ((line = reader.readLine()) != null) { listElementItem.add(line); } is.close(); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { if (adapter == null) { adapter = new ArrayAdapter(DropboxSampleActivity.this, android.R.layout.simple_list_item_1, listElementItem); runOnUiThread(new Runnable() { @Override public void run() { listViewDropbox.setAdapter(adapter); } }); } else { runOnUiThread(new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); } }); } } }; 

}

+2
source

Source: https://habr.com/ru/post/1241146/


All Articles