如何从Android模拟器中选择文件?

时间:2012-01-04 05:35:00

标签: android

这些代码似乎没有错误但是当我点击“选择文件”按钮时活动停止。我希望将文件上传到Amazon S3,但我必须在上传之前选择一个文件。我希望上传的文件来自模拟器的“下载”。 谁知道什么是错的?

  package sit.nyp.edu.sg.filepicker;

  import java.io.File;

  import android.app.Activity;
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;

    public class FilePicker2Activity extends Activity {
/** Called when the activity is first created. */
TextView textFile, textFileName, textFolder;
TextView textFileName_WithoutExt, textFileName_Ext;

private static final int PICKFILE_RESULT_CODE = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button buttonPick = (Button)findViewById(R.id.buttonpick);
    textFile = (TextView)findViewById(R.id.textfile);
    textFolder = (TextView)findViewById(R.id.textfolder);
    textFileName = (TextView)findViewById(R.id.textfilename);

    textFileName_WithoutExt = (TextView)findViewById(R.id.textfilename_withoutext);
    textFileName_Ext = (TextView)findViewById(R.id.textfilename_ext);

    buttonPick.setOnClickListener(new Button.OnClickListener(){

        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setAction(Intent.ACTION_PICK);
            intent.setType("file/*");
            startActivityForResult(intent,PICKFILE_RESULT_CODE);



        }});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    switch(requestCode){
    case PICKFILE_RESULT_CODE:
        if(resultCode==RESULT_OK){

            String FilePath = data.getData().getPath();
            String FileName = data.getData().getLastPathSegment();
            int lastPos = FilePath.length() - FileName.length();
            String Folder = FilePath.substring(0, lastPos);

            textFile.setText("Full Path: \n" + FilePath + "\n");
            textFolder.setText("Folder: \n" + Folder + "\n");
            textFileName.setText("File Name: \n" + FileName + "\n");

            filename thisFile = new filename(FileName);
            textFileName_WithoutExt.setText("Filename without Ext: " + thisFile.getFilename_Without_Ext());
            textFileName_Ext.setText("Ext: " + thisFile.getExt());

        }
        break;

    }
}

private class filename{

    String filename_Without_Ext = "";
    String ext = "";

    filename(String file){
        int dotposition= file.lastIndexOf(".");
        filename_Without_Ext = file.substring(0,dotposition);
        ext = file.substring(dotposition + 1, file.length());  
    }

    String getFilename_Without_Ext(){
        return filename_Without_Ext;
    }

    String getExt(){
        return ext;
    }
}

}

1 个答案:

答案 0 :(得分:1)

我的猜测是,你没有任何可以在模拟器上处理获取内容意图的文件浏览器应用程序,但是如果你发布日志,可以清楚地得出结论。