0

私はこのファイル ピッカーをオンラインで見つけました。開発者は、必要に応じて使用できると述べています。コードが理解しやすいと思ったので、それを使用して、アプリケーション用に少し変更することにしました。すべてのクレジットは元の開発者 ( https://github.com/mburman/Android-File-Explore )に帰属します

        loadFileList();

    showDialog(DIALOG_LOAD_FILE);
    Log.d(TAG, path.getAbsolutePath());

}

private void loadFileList() {
    try {
        path.mkdirs();
    } catch (SecurityException e) {
        Log.e(TAG, "unable to write on the sd card ");
    }

    // Checks whether path exists
    if (path.exists()) {
        FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String filename) {
                File sel = new File(dir, filename);
                // Filters based on whether the file is hidden or not
                return (sel.isFile() || sel.isDirectory())
                        && !sel.isHidden();

            }
        };

        String[] fList = path.list(filter);
        fileList = new Item[fList.length];
        for (int i = 0; i < fList.length; i++) {
            fileList[i] = new Item(fList[i], R.drawable.file_icon);

            // Convert into file path
            File sel = new File(path, fList[i]);

            // Set drawables
            if (sel.isDirectory()) {
                fileList[i].icon = R.drawable.directory_icon;
                Log.d("DIRECTORY", fileList[i].file);
            } else {
                Log.d("FILE", fileList[i].file);
            }
        }

        if (!firstLvl) {
            Item temp[] = new Item[fileList.length + 1];
            for (int i = 0; i < fileList.length; i++) {
                temp[i + 1] = fileList[i];
            }
            temp[0] = new Item("Up", R.drawable.directory_up);
            fileList = temp;
        }
    } else {
        Log.e(TAG, "path does not exist");
    }

    adapter = new ArrayAdapter<Item>(this,
            android.R.layout.select_dialog_item, android.R.id.text1,
            fileList) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // creates view
            View view = super.getView(position, convertView, parent);
            TextView textView = (TextView) view
                    .findViewById(android.R.id.text1);

            // put the image on the text view
            textView.setCompoundDrawablesWithIntrinsicBounds(
                    fileList[position].icon, 0, 0, 0);

            // add margin between image and text (support various screen
            // densities)
            int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
            textView.setCompoundDrawablePadding(dp5);

            return view;
        }
    };

}

private class Item {
    public String file;
    public int icon;

    public Item(String file, Integer icon) {
        this.file = file;
        this.icon = icon;
    }

    @Override
    public String toString() {
        return file;
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    AlertDialog.Builder builder = new Builder(this);

    if (fileList == null) {
        Log.e(TAG, "No files loaded");
        dialog = builder.create();
        return dialog;
    }

    switch (id) {
    case DIALOG_LOAD_FILE:
        builder.setTitle("Choose your file");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                chosenFile = fileList[which].file;
                File sel = new File(path + "/" + chosenFile);
                if (sel.isDirectory()) {
                    firstLvl = false;

                    // Adds chosen directory to list
                    str.add(chosenFile);
                    fileList = null;
                    path = new File(sel + "");

                    loadFileList();

                    removeDialog(DIALOG_LOAD_FILE);
                    showDialog(DIALOG_LOAD_FILE);
                    Log.d(TAG, path.getAbsolutePath());



                }

                // Checks if 'up' was clicked
                else if (chosenFile.equalsIgnoreCase("up") && !sel.exists()) {

                    // present directory removed from list
                    String s = str.remove(str.size() - 1);

                    // path modified to exclude present directory
                    path = new File(path.toString().substring(0,
                            path.toString().lastIndexOf(s)));
                    fileList = null;

                    // if there are no more directories in the list, then
                    // its the first level
                    if (str.isEmpty()) {
                        firstLvl = true;
                    }
                    loadFileList();

                    removeDialog(DIALOG_LOAD_FILE);
                    showDialog(DIALOG_LOAD_FILE);
                    Log.d(TAG, path.getAbsolutePath());

                }

                // File picked
                else {
                    chosenFile = fileList[which].file;
                    File test = new File(path + "/" + chosenFile);

                    sendback(test);

                }
            }
        });
        break;
    }
    dialog = builder.show();
    return dialog;
}

申し訳ありませんが、少し長すぎます。しかし、それは onCreate() にあるファイル ピッカー全体です。次のコードを使用して開きます。

Bundle b = new Bundle();
            b.putInt("tallet", 1);
            Intent i = new Intent(getApplicationContext(), FileExplore.class);
            i.putExtras(b);
            startActivityForResult(i, 0);

コードは完全に機能します。しかし、「戻る」(onBackPressed) を押すと、finish(); と言うと空白の (黒い) 画面が表示されます。. 現在、私はこのコードを使用しています:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    Intent backIntent = new Intent(FileExplore.this, AndroidTabLayoutActivity.class);
     startActivity(backIntent); 
} 

これは実際には機能しますが、一度戻るとメニューが表示され、もう一度戻るボタンを押すと黒い画面が表示されます。編集:最初ではなく、2番目のバックプレスでonBackPressedコードに入ります。

これが PhotosActivity の onActivityResult コードです (これはタブレイアウトです - photosactivity は画面の 1 つにすぎません)。

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        //  Bundle b = getIntent().getExtras();

        if (resultCode == -1) 
        {
         Bundle b =  data.getBundleExtra("FiletoPhoto");

                int knappen = b.getInt("number");
            String titlen = b.getString("title");
                        {
                            Listofsounds los = new Listofsounds();
                        String shortname    = los.puttextonit(titlen, knappen);

                    putnameinit(shortname,knappen);
                        }
        }
        else if (resultCode == 0)
        {
            //If I press "back" i've made the resultCode to be 0 in the onBackPressed. What should I do here then?
        }

    }  

ボタンを 1 回押すだけでメニューに戻るにはどうすればよいですか?

4

1 に答える 1

0

問題は、このコードを使用する場合です。

 Intent backIntent = new Intent(FileExplore.this, AndroidTabLayoutActivity.class);
 startActivity(backIntent);

基本的に、すでに存在する AndroidTabLayoutActivity の新しいアクティビティを作成して開始しているため、このアクティビティの 2 つのコピーが同時に実行されます。ただし、startActivityForResult() を呼び出したので、元のアクティビティは結果を期待しています。問題の解決策は、onActivityResult として返される resultCode が RESULT_OK であるか、または FileExplore の setResult(...) で設定している成功した resultCode であるかどうかを確認することだと思います。

私のすべてのアプリで onBackPressed をオーバーライドしていないにもかかわらず、奇妙な副作用を引き起こすことなくアクティビティを終了できるため、これは非常に奇妙です。

于 2012-08-23T16:21:55.283 に答える