从模拟器的存储中搜索特定的文件类型[android studio] [英] Searching specific file types from an emulator's storage [android studio]

查看:59
本文介绍了从模拟器的存储中搜索特定的文件类型[android studio]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手,我正在尝试构建一个图书列表应用程序。我通过命令行在我的模拟器的SD卡中推送了一些文件,但是它们出现在它的存储器中,你可以找到多个文件夹,如下载(我的文件所在的位置),警报,音乐等。



现在我正试图找到一种方法来访问它并通过FloatingActionButton加载特定的书籍类型(例如mobi和epud)。



我有在我的AndroidManifest中读取和写入权限中的外部存储空间,这是我的MainActivity,因为它现在看起来很像编辑输入和输出代码,可以帮助



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

//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);
        }
    }
}





我的智慧结束了...我已经一直试图找到一个解决方案,我正在寻求帮助2天,我不能再多花2天以上'因为我有很多其他项目要做(opengl,dreamweaver,更多java just)不在Android工作室等)所以任何帮助将不胜感激= D



我尝试过:



几乎所有我尝试的都在代码注释或工作中



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

推荐答案

好的,所以当你说推你我正在谈论一个adb push命令 - 这与我认为你可能会谈论的PUSH服务有很大不同。



让我们先考虑你的第二个问题:如何进行文件过滤。使用 Apache Commons IO File有一个简单的解决方案公用事业的。这是一个可以添加到项目中的额外Android库,它将补充SDK并提供一组非常有用的文件相关实用程序,这将使您更容易实现所需的过滤。



我知道你想快速包装它,所以你可能不想采用新的库方法。在这种情况下,您需要使用调试器并自己完成它。只需设置一些断点并从那里开始单步。



现在关于推送文件的问题。您可以再次使用调试器(或android.util.Log)来确认设备外部存储区域的绝对路径。它可能在/ storage / sdcard0上提供。使用adb shell命令打开shell并检查是否可以导航到该目录(例如cd / storage / sdcard0)并在那里列出一些文件(ls -l)。假设您可以退出shell并按下绝对路径(adb push< your files>< ext path to ext storage>)。
OK, so when you say "push" you're talking about an adb push command - that's very different from the kind of PUSH service that I thought you might be talking about.

Let's think about your second question first: how to do the file filtering. There's an easy solution using the Apache Commons IO File Utilities. This is an additional Android library that you can add to your project, it will supplement the SDK and provides a set of very useful file-related utilities which will make it easier to achieve the filtering that you want.

I know that you want to wrap this up quickly so you might not want to go with the new library approach. In that case you need to use the debugger and work through it yourself. Just set some breakpoints and single step from there.

Now the question about pushing files. You can again use the debugger (or android.util.Log) to confirm the absolute path to your device's external storage area. It's likely available at /storage/sdcard0. Use the "adb shell" command to open a shell and check that you can navigate to that directory (e.g. "cd /storage/sdcard0") and list some files there ("ls -l"). Assuming that you can then quit the shell and do your push to that absolute path ("adb push <your files> <absolute path to ext storage>").


这篇关于从模拟器的存储中搜索特定的文件类型[android studio]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆