0

我有代码在带有复选框的列表视图中显示 SD 卡的文件。它可能会加载完整的文件,而不仅仅是它们的路径!我希望用户选择两个文件,然后获取输入流的文件路径以将它们合并到第三个文件。

代码是 MainActivity.java

package com.example.joiner;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.List;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button mButton;
    private List<Model> list;
private ListView mainListView ;
 public String msg="kumkum";
 File[] sdDirectories;
 ArrayList<String> planetList;
 private List<File> h;




 ArrayAdapter<Model> adapter ;



    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton = (Button)findViewById(R.id.Button);
        //mButton.setOnClickListener(this);
        mainListView = (ListView) findViewById( R.id.mainListView );
        mButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                int temp;
                //Model[] a=(Model[]) list.toArray();
                for(int i=0;i<list.size();i++)
                {
                    Model b=list.get(i);
                    if(b.isSelected())
                    {
                        //Toast.makeText(MainActivity.this, b.getName(), Toast.LENGTH_LONG).show();
                        for(int counter=0;counter<sdDirectories.length;counter++)
                        {
                            if(b.getName().equals(sdDirectories[counter].getName()))
                            {
                                Toast.makeText(MainActivity.this,sdDirectories[counter].getPath(), Toast.LENGTH_LONG).show();
                                h.add(sdDirectories[counter]);
                                //h.get(0).getPath();
                            }
                        }
                    }
                }

            }



        });
        String extState = Environment.getExternalStorageState();
        //you may also want to add (...|| Environment.MEDIA_MOUNTED_READ_ONLY)
        //if you are only interested in reading the filesystem
        if(!extState.equals(Environment.MEDIA_MOUNTED)) {
          Toast.makeText(MainActivity.this, "no sd card found", Toast.LENGTH_LONG).show();

        }
        else{




            planetList = new ArrayList<String>();
            planetList.add("wajeeh");



            //for(int i=0;i<sdDirectories.length;i++)
            //planetList.add(sdDirectories[i].getName());



            planetList.add(msg);

            adapter = new Inadopter(MainActivity.this,getModel());



// Set the ArrayAdapter as the ListView's adapter.
            mainListView.setAdapter( adapter );



          }
    }
    private List<Model> getModel() {
        list = new ArrayList<Model>();
        list.add(get("Linux"));
        list.add(get("a"));
        // Initially select one of the items
        File sd = Environment.getExternalStorageDirectory();
        FileFilter MP3ANDdirectoryOnly = new FileFilter() {
            private String getExtension(File f) {
                String ext = null;
                String s = f.getName();
                int i = s.lastIndexOf('.');

                if (i > 0 &&  i < s.length() - 1) {
                    ext = s.substring(i+1).toLowerCase();
                }
                return ext;
            }

            public boolean accept(File file) {
                if(file.isDirectory()||!file.isHidden())
                    return true;

                String extension = getExtension(file);
                if (extension != null||!file.isHidden()) {
                    if (extension.equals("mp3"))
                        {
                            return true;
                    }
            }
                return false;
        }};
        sdDirectories = sd.listFiles(MP3ANDdirectoryOnly);
        for(int i=0;i<sdDirectories.length;i++)
            list.add(get(sdDirectories[i].getName()));


        list.get(1).setSelected(true);
        return list;
      }
    private Model get(String s) {
        // TODO Auto-generated method stub
        return new Model(s);
    }
}

`

InAdapter.java

    package com.example.joiner;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class Inadopter extends ArrayAdapter<Model> {

  private final List<Model> list;
  private final Activity context;

  public Inadopter(Activity context, List<Model> list) {
    super(context, R.layout.simpleroow, list);
    this.context = context;
    this.list = list;
  }

  static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
      LayoutInflater inflator = context.getLayoutInflater();
      view = inflator.inflate(R.layout.simpleroow, null);
      final ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) view.findViewById(R.id.label);
      viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
      viewHolder.checkbox
          .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
              Model element = (Model) viewHolder.checkbox
                  .getTag();
              element.setSelected(buttonView.isChecked());

            }
          });
      view.setTag(viewHolder);
      viewHolder.checkbox.setTag(list.get(position));
    } else {
      view = convertView;
      ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());
    holder.checkbox.setChecked(list.get(position).isSelected());
    return view;
  }
} 

我需要获取两个文件的路径并将其发送到此代码

public class ConcateSongActivity extends Activity {
    Button mbutt;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mbutt = (Button)findViewById(R.id.button_Click_Karo);
        mbutt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                try {
                    FileInputStream fis1 = new FileInputStream("/sdcard/viv0.wav");
                    FileInputStream fis2 = new FileInputStream("/sdcard/viv1.wav");
                    SequenceInputStream sis = new SequenceInputStream(fis1,fis2);


                    FileOutputStream fos = new FileOutputStream(new File("/sdcard/vis.wav"));

                    int temp;

                    try {
                        while ((temp = sis.read())!= -1){

                            fos.write(temp);

                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }




                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
        });
    }
}
4

1 回答 1

0

随着反馈链接更改为问题“这篇文章对您有用吗?” 当我尝试点击它时,除了在此处留作回答的地方提出问题外,我别无他法。

你的问题到底是什么?你不能提取两个路径还是不能合并两个文件?

于 2012-12-09T16:46:16.337 回答