I'm new to android development and I'm trying to build a book listing app. I pushed some files in my emulator's sdcard through command line, but they appeared in its storage where you can find multiple folders like Downloads (where my files are), Alarms, Music etc.
Now I'm trying to find a way to access it and load specific book types (such as mobi and epud) through a FloatingActionButton.
I have read and write external storage in permissions in my AndroidManifest and this is my MainActivity as it looks like now through alot of editing in and out code that could help
package com.iekproject.siegfried.libraryapp;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
implements LibraryFragment.LibraryFragmentListener, DetailFragment.DetailFragmentListener,
AddEditFragment.AddEditFragmentListener {
private static final String AUTHORITY = "com.iekproject.siegfried.libraryapp";
public static final String BOOK_URI = "book_uri";
private LibraryFragment libraryFragment;
private File root;
private ArrayList<File> fileList = new ArrayList<File>();
private LinearLayout view;
FloatingActionButton btn;
int PICKFILE_RESULT_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null && findViewById(R.id.fragmentContainer) != null) {
libraryFragment = new LibraryFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragmentContainer, libraryFragment);
transaction.commit();
}
else {
libraryFragment =
(LibraryFragment) getSupportFragmentManager().
findFragmentById(R.id.DetailFragment);
}
root = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath());
getfile(root);
for (int i = 0; i < fileList.size(); i++) {
TextView textView = new TextView(this);
textView.setText(fileList.get(i).getName());
textView.setPadding(5, 5, 5, 5);
System.out.println(fileList.get(i).getName());
if (fileList.get(i).isDirectory()) {
textView.setTextColor(Color.parseColor("#FF0000"));
}
view.addView(textView);
}
btn = (FloatingActionButton) findViewById(R.id.addButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
}
});
}
public ArrayList<File> getfile(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
fileList.add(listFile[i]);
getfile(listFile[i]);
}
else {
if (listFile[i].getName().endsWith(".mobi")
|| listFile[i].getName().endsWith(".epub")) {
fileList.add(listFile[i]);
}
}
}
}
return fileList;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==PICKFILE_RESULT_CODE){
Log.d("TAG", "File Uri " +data.getData());
}
}
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
@Override
public void onBookSelected(Uri bookUri) {
getSupportFragmentManager().popBackStack();
displayBook(bookUri, R.id.rightPaneContainer);
}
@Override
public void onAddBook() {
displayAddEditFragment(R.id.rightPaneContainer, null);
}
private void displayBook(Uri bookUri, int viewID) {
DetailFragment detailFragment = new DetailFragment();
Bundle arguments = new Bundle();
arguments.putParcelable(BOOK_URI, bookUri);
detailFragment.setArguments(arguments);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(viewID, detailFragment);
transaction.addToBackStack(null);
transaction.commit();
}
private void displayAddEditFragment(int viewID, Uri bookUri) {
AddEditFragment addEditFragment = new AddEditFragment();
if (bookUri != null) {
Bundle arguments = new Bundle();
arguments.putParcelable(BOOK_URI, bookUri);
addEditFragment.setArguments(arguments);
}
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(viewID, addEditFragment);
transaction.addToBackStack(null);
transaction.commit();
}
@Override
public void onBookDeleted() {
getSupportFragmentManager().popBackStack();
libraryFragment.updateLibrary();
}
@Override
public void onAddEditCompleted(Uri bookUri) {
getSupportFragmentManager().popBackStack();
libraryFragment.updateLibrary();
if (findViewById(R.id.fragmentContainer) == null){
getSupportFragmentManager().popBackStack();
displayBook(bookUri, R.id.rightPaneContainer);
}
}
}
I am at my wits end... I've been trying to find a solution to just what I'm asking help for for like 2 days and I can't spend more than 2 more days on it 'cos I have alot of other projects to do (opengl, dreamweaver, more java just not in android studio etc) so any help will be greatly appreciated =D
What I have tried:
Pretty much everything I have tried are in the code commented or at work