Created
November 26, 2017 20:14
-
-
Save MacoChave/aa39d211b3b66a90b7feeb7771fbd938 to your computer and use it in GitHub Desktop.
FileChooser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.macochave.tupperinventario; | |
import android.app.Fragment; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import com.macochave.tupperinventario.adaptador.FileAdapter; | |
import com.macochave.tupperinventario.item.ListViewItem; | |
import com.macochave.tupperinventario.mensaje.MessageEvent; | |
import org.greenrobot.eventbus.EventBus; | |
import java.io.File; | |
import java.util.ArrayList; | |
public class FileChooserFragment extends Fragment implements AdapterView.OnItemClickListener { | |
private String DIRECTORIO; | |
private ListView lista; | |
private ArrayList<ListViewItem> items; | |
private FileAdapter adaptador; | |
private EventBus bus = EventBus.getDefault(); | |
public FileChooserFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
View view = inflater.inflate(R.layout.fragment_file_chooser, container, false); | |
lista = view.findViewById(R.id.list_files); | |
items = new ArrayList<>(); | |
DIRECTORIO = Environment.getExternalStorageDirectory().getPath(); | |
Log.i("DIRECTORIO", "onCreateView: " + DIRECTORIO); | |
listarArchivos(DIRECTORIO); | |
lista.setOnItemClickListener(this); | |
return view; | |
} | |
private void listarArchivos(String raiz) { | |
File file = new File(raiz); | |
File[] files = file.listFiles(); | |
try { | |
items.add( | |
new ListViewItem(R.drawable.ic_folder_open_rosa_75dp, "Subir carpeta", file.getParentFile().getPath()) | |
); | |
} | |
catch (Exception ex) | |
{ | |
items.add( | |
new ListViewItem(R.drawable.ic_folder_open_rosa_75dp, "Subir carpeta", file.getPath()) | |
); | |
} | |
for (File f : files) | |
{ | |
ListViewItem item = new ListViewItem( | |
R.drawable.ic_folder_rosa_75dp, | |
f.getName(), | |
f.getPath() | |
); | |
if (f.isFile()) | |
{ | |
if (item.getTexto1().endsWith("jpg") || item.getTexto1().endsWith("JPG") || item.getTexto1().endsWith("jpeg") || item.getTexto1().endsWith("JPEG")) | |
item.setEsImagen(true); | |
item.setImagen(R.drawable.ic_description_rosa_75dp); | |
} | |
items.add(item); | |
} | |
} | |
@Override | |
public void onDetach() { | |
super.onDetach(); | |
} | |
@Override | |
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { | |
ListViewItem item = (ListViewItem) adapterView.getItemAtPosition(i); | |
File file = new File(item.getTexto2()); | |
if (file.isFile()) | |
{ | |
bus.postSticky(new MessageEvent(5, file.getPath())); | |
getFragmentManager().popBackStack(); | |
} | |
else | |
{ | |
adaptador = null; | |
items.clear(); | |
lista.setAdapter(adaptador); | |
listarArchivos(item.getTexto2()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment