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 internal storage.
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
//MainActivity.java
//Hosts the app's fragments and handles communication between them
//I'm skipping the phone parts of the app since my revised one is for tablets only
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";
//key for storing a book's Uri in a Bundle passed to a fragment
public static final String BOOK_URI = "book_uri";
private LibraryFragment libraryFragment; //displays library aka book list
private File root;
private ArrayList<File> fileList = new ArrayList<File>();
private LinearLayout view;
FloatingActionButton btn;
int PICKFILE_RESULT_CODE=1;
//displays LibraryFragment when MainActivity first loads
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
setContentView(R.layout.activity_main);
//if layout contains fragmentContainer, the phone layout is in use. Create and display
//a LibraryFragment
if (savedInstanceState == null && findViewById(R.id.fragmentContainer) != null) {
//create LibraryFragment
libraryFragment = new LibraryFragment();
//add the fragment to the FrameLayout
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragmentContainer, libraryFragment);
transaction.commit(); //displays LibraryFragment
}
else {
libraryFragment =
(LibraryFragment) getSupportFragmentManager().
findFragmentById(R.id.DetailFragment);
}
//view = (LinearLayout) findViewById(R.id.DetailFragment);
//getting SDcard root path
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;
}
//displays DetailFragment for selected book
@Override
public void onBookSelected(Uri bookUri) {
getSupportFragmentManager().popBackStack();
displayBook(bookUri, R.id.rightPaneContainer);
}
//displays AddEditFragment to add a new book. Possibly what I'll also have to change to make it
//scan/update the book list
@Override
public void onAddBook() {
displayAddEditFragment(R.id.rightPaneContainer, null);
}
//displays a book
private void displayBook(Uri bookUri, int viewID) {
DetailFragment detailFragment = new DetailFragment();
//specify book's Uri as an argument to the DetailFragment
Bundle arguments = new Bundle();
arguments.putParcelable(BOOK_URI, bookUri);
detailFragment.setArguments(arguments);
//use a FragmentTransaction to display the DetailFragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(viewID, detailFragment);
transaction.addToBackStack(null);
transaction.commit(); //causes DetailFragment to display
}
//displays fragment for adding new or editing existing book
private void displayAddEditFragment(int viewID, Uri bookUri) {
AddEditFragment addEditFragment = new AddEditFragment();
//if editing existing book, provide bookUri as an argument
if (bookUri != null) {
Bundle arguments = new Bundle();
arguments.putParcelable(BOOK_URI, bookUri);
addEditFragment.setArguments(arguments);
}
//use a FragmentTransaction to display the AddEditFragment
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(viewID, addEditFragment);
transaction.addToBackStack(null);
transaction.commit(); //causes AddEditFragment to display
}
//return to book list when displayed book deleted
@Override
public void onBookDeleted() {
//removes top of back stack
getSupportFragmentManager().popBackStack();
libraryFragment.updateLibrary(); //refresh book list
}
//displays the AddEditFragment to edit an existing book. Maybe it can be used as Move or sth
/*@Override
public void onEditBook(Uri bookUri) {
displayAddEditFragment(R.id.rightPaneContainer, bookUri);
}*/
//update GUI after the new book or updated book saved
@Override
public void onAddEditCompleted(Uri bookUri) {
//removes top of back stack
getSupportFragmentManager().popBackStack();
libraryFragment.updateLibrary(); //refresh book list
if (findViewById(R.id.fragmentContainer) == null){ //tablet
//removes top of back stack
getSupportFragmentManager().popBackStack();
//on tablet, displays the book that was just added or edited
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
EDIT: I was informed that Internal storage is part of the memory system that is “local” to an app, which cleared some things for me. I pushed some books through command line on the emulator that were supposed to appear in the sdcard but they appear instead in Storage (through the emulator’s settings app) and in Android SDK built for x86 which has multiple folders one of which is Downloads and there my files are. Hope this clears some things that might have otherwise confuse