Android - AYUDA EXPLORADOR ARCHIVOS

 
Vista:

AYUDA EXPLORADOR ARCHIVOS

Publicado por Paty (1 intervención) el 08/03/2018 02:04:50
HOLAA!!! Soy nueva en el desarrollo de aplicaciones :D tengo una app que genera una orden de servicio en pdf para después ser enviada por correo, la cosa es que quiero poder listar las ordenes ya generadas y poder acceder a ellas con un click, o explorar la carpeta donde se crean, en mi caso se crean en Downloads/ORDENES DE SERVICIO, mi problema es que al construir el explorador, no me lleva a ningún lado, si pudieran sujerirme algún codigo para enlistarlos en la app estaria genial, si me ayudan a resolver este problema estaria igual de genial!! gracias!!!

les paso el codigo de mi main activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.ranitaapps.myapplication;
 
package com.ranitaapps.myapplication;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
 
public class MainActivity extends AppCompatActivity {
 
    private static final int REQUEST_PATH = 1;
    String cirFileName;
    EditText editText;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
 
    }
 
    public void getfile(View view){
        Intent intent1 = new Intent(MainActivity.this,FileChooser.class);
        startActivity(intent1);
    }
 
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode==REQUEST_PATH){
            if(resultCode==RESULT_OK){
                cirFileName=data.getStringExtra("GetFileName");
                editText.setText(cirFileName);
            }
        }
    }
}



este es de Item


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.ranitaapps.myapplication;
 
 
 
public class Item implements Comparable<Item> {
    private final String name;
 
    private final String date;
    private final String image;
    private final String path;
    private final String data;
 
 
    public Item(String n, String d, String dt, String p, String img)
    {
        name=n;
        date=dt;
        path=p;
        image=img;
        data=d;
 
 
    }
 
    public String getName() {return name;}
    public String getData() {return data;}
    public String getDate() {return date;}
    public String getPath() {return path;}
    public String getImage() {return image;}
 
    public int compareTo(Item o) {
        if(Item.this.name != null)
            return this.name.toLowerCase().compareTo(o.getName().toLowerCase());
        else
            throw new IllegalArgumentException();
    }
}


en donde elijo el archivo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.ranitaapps.myapplication;
 
 
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
 
import java.io.File;
import java.text.DateFormat;
import java.util.ArrayList;
import java.sql.Date;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
 
public class FileChooser extends ListActivity {
 
    private File currentDir;
    private FileArrayAdapter adapter;
 
 
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        currentDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "ORDENES DE SERVICIO");
        fill(currentDir);
    }
    private void fill(File f){
         File[]dirs = f.listFiles();
         this.setTitle("Current Dir: "+f.getName());
        List<Item>dir = new ArrayList<Item>();
        List<Item>fls = new ArrayList<Item>();
        try{
            for(File ff: dirs){
                Date lasModFste = new Date(ff.lastModified());
                DateFormat formater = DateFormat.getDateTimeInstance();
                String date_modify = formater.format(lasModFste);
                if(ff.isDirectory()){
 
                    File[] fbuf = ff.listFiles();
                    int buf = 0;
                    if(fbuf!= null){
                        buf = fbuf.length;
                    }
                    else
                        buf=0;
                        String num_item = String.valueOf(buf);
                        if(buf==0) num_item = num_item + " item";
                        else num_item = num_item + " items";
                        dir.add(new Item(ff.getName(), num_item,date_modify,ff.getAbsolutePath(),"directory_icon"));
                    }
                    else {
                        fls.add(new Item(ff.getName(),ff.length() + " Byte", date_modify,ff.getAbsolutePath(),"file_icon"));
                    }
                }
            }catch (Exception e){
        }
        Collections.sort(dir);
            Collections.sort(fls);
            dir.addAll(fls);
            if(!f.getName().equals("/downloads"))
                dir.add(0,new Item(". .","Parent Directory","",f.getParent(),"directory_up"));
            adapter = new FileArrayAdapter(FileChooser.this,R.layout.file_view,dir);
            this.setListAdapter(adapter);
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Item o = adapter.getItem(position);
        if (o.getImage().equals("directory_icon") || o.getImage().equalsIgnoreCase("directory_up")) {
            ;
            currentDir = new File(o.getPath());
            fill(currentDir);
        } else {
            onFileClick(o);
 
        }
    }
    private void onFileClick(Item o)
    {
        Intent intent = new Intent();
        intent.putExtra("GetPath",currentDir.toString());
        intent.putExtra("GetFileName",o.getName());
        setResult(RESULT_OK, intent);
        finish();
 
    }
 
 
}


//mi adaptador

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.ranitaapps.myapplication;
 
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
import java.util.List;
 
 
public class FileArrayAdapter extends ArrayAdapter<Item> {
 
    private Context c;
    private int id;
    private List<Item> items;
 
 
    public FileArrayAdapter(Context context, int textViewResourceId, List<Item> objects) {
        super(context, textViewResourceId, objects);
        c = context;
        id = textViewResourceId;
        items = objects;
    }
 
    public Item getItem(int i) {
        return items.get(i);
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(id, null);
        }
 
        final Item o = items.get(position);
        if (o != null) {
            TextView t1 = v.findViewById(R.id.TextView01);
            TextView t2 = v.findViewById(R.id.TextView02);
            TextView t3 = v.findViewById(R.id.TextView03);
 
            ImageView imageCity = v.findViewById(R.id.fd_Icon1);
            String uri = "drawable/" + o.getImage();
            int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
            Drawable image = c.getResources().getDrawable(imageResource);
            imageCity.setImageDrawable(image);
 
            if (t1 != null)
                t1.setText(o.getName());
 
            if (t2 != null)
                t2.setText(o.getData());
 
            if (t3 != null)
                t3.setText(o.getDate());
            }
            return v;
        }
    }
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder