如何共享多个文件?

时间:2017-02-01 04:36:59

标签: java android

我已经对选定的多个listview项目进行了编码,但问题是当我选择多个项目时,它只共享一个项目,其余项目未被选中。请帮我分享多个listview项目。< / p>

public class MainActivity extends AppCompatActivity {

TextView textView;
ListView listView;
List<String> myList;
String MEDIA_PATHS;
Uri uri;

int count = 0;
ArrayList<String> arrayList2 = new ArrayList<>();

String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() + "/CallLogs" ); 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView)findViewById(R.id.list_items);
    textView = (TextView)findViewById(R.id.textView);

    myList = new ArrayList<String>();

    final File files = new File(MEDIA_PATH);

    File list[] = files.listFiles();
    for (int i = 0; i < list.length; i++) {
        myList.add(list[i].getName());
    }

    final ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list_layout,R.id.textView,myList);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int position, long l, boolean b) {

            count = count+1;
            actionMode.setTitle(count + " items selected");
            MEDIA_PATHS = new String(Environment.getExternalStorageDirectory() + "/CallLogs/"  + myList.get(position));
        //    arrayList2.add(myList.get(position));
            arrayList2.add(MEDIA_PATH);

        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            MenuInflater inflater = actionMode.getMenuInflater();
            inflater.inflate(R.menu.list_menu,menu);

            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {

            switch (menuItem.getItemId())
            {

                case R.id.delete_id:

                    for(String msg : arrayList2) {
                        adapter.remove(msg);

                    }
                    Toast.makeText(getApplicationContext(),"deleted",Toast.LENGTH_SHORT).show();
                    count=0;
                    actionMode.finish();
                    return true;
                //  break;

                case R.id.share_id:

                    for(String msg : arrayList2) {
                        uri = Uri.parse(msg);
                    }
              //      Uri uri = Uri.parse(MEDIA_PATHS);

                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.putExtra(Intent.EXTRA_STREAM, uri);
                    share.setType("audio/*");

                    share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(Intent.createChooser(share, "Share audio File"));

                    return true;

                default:
                    Toast.makeText(getApplicationContext(),"Nothing selected",Toast.LENGTH_SHORT).show();
                    break;
            }

            return false;
        }


        @Override
        public void onDestroyActionMode(ActionMode actionMode) {

        }
    });

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

          //  myList.get(position);
            adapterView.setSelected(true);
        }
    });

}

}

2 个答案:

答案 0 :(得分:1)

Im提供的示例是我使用的电子邮件附件。 它将使用Intent为您提供想法。

final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
ei.setType("plain/text");
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"me@somewhere.nodomain"});
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");

添加文件'uris:

ArrayList<Uri> uris = new ArrayList<Uri>();

ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);

答案 1 :(得分:0)

请参阅此链接,了解有关共享文件的更多信息。我试过,对我有用。

https://developer.android.com/training/sharing/send.html