-1

也许这是一个愚蠢的问题,但我正在通过使用文件选择器选择文件来执行上传文件功能,但我不知道如何获取文件路径。

请帮忙。

public class UploadToServer extends Activity {

EditText fileSaveLoad;
TextView messageText;
Button uploadButton;
int serverResponseCode = 0;
ProgressDialog dialog = null;

String upLoadServerUri = null;

private Button mLoadButton;
private Button mSaveButton;

/** Sample filters array */
final String[] mFileFilter = { "*.*", ".jpeg", ".txt", ".png" };


/**********  File Path *************/
final String uploadFilePath = "/mnt/sdcard/";
final String uploadFileName = "test.png";
        //fileSaveLoad.getText().toString();
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload_to_server);

    fileSaveLoad = (EditText)findViewById(R.id.fileSaveLoad);
    uploadButton = (Button)findViewById(R.id.uploadButton);
    messageText  = (TextView)findViewById(R.id.messageText);
    mLoadButton = (Button) findViewById(R.id.button_load);
    mSaveButton = (Button) findViewById(R.id.button_save);

    messageText.setText("Uploading file path :- '/mnt/sdcard/"+uploadFileName+"'");

    /************* Php script path ****************/
    upLoadServerUri = "http://192.168.0.106/UploadToServer.php";

    uploadButton.setOnClickListener(new OnClickListener() {            
        @Override
        public void onClick(View v) {

            dialog = ProgressDialog.show(UploadToServer.this, "", "Uploading file...", true);

            new Thread(new Runnable() {
                    public void run() {
                         runOnUiThread(new Runnable() {
                                public void run() {
                                    messageText.setText("uploading started.....");
                                }
                            });                      

                         uploadFile(uploadFilePath + "" + uploadFileName);

                    }
                  }).start();        
            }
        });

  mLoadButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            new FileSelector(UploadToServer.this, FileOperation.LOAD, mLoadFileListener, mFileFilter).show();
        }
    });

    mSaveButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            new FileSelector(UploadToServer.this, FileOperation.SAVE, mSaveFileListener, mFileFilter).show();
        }
    });
}



OnHandleFileListener mLoadFileListener = new OnHandleFileListener() {
    @Override
    public void handleFile(final String filePath) {
        Toast.makeText(UploadToServer.this, "Load: " + filePath, Toast.LENGTH_SHORT).show();
    }
};

OnHandleFileListener mSaveFileListener = new OnHandleFileListener() {
    @Override
    public void handleFile(final String filePath) {
        Toast.makeText(UploadToServer.this, "Save: " + filePath, Toast.LENGTH_SHORT).show();
    }
};

我想问一下,有什么办法可以得到filePath

OnHandleFileListener mLoadFileListener = new OnHandleFileListener() {
@Override
public void handleFile(final String filePath) {
    Toast.makeText(UploadToServer.this, "Load: " + filePath, Toast.LENGTH_SHORT).show();
}
};

 final String uploadFileName = "test.png";
4

1 回答 1

0

最简单的做法是将此值分配给类成员

public class UploadToServer extends Activity {

EditText fileSaveLoad;
TextView messageText;
Button uploadButton;
int serverResponseCode = 0;
ProgressDialog dialog = null;

String upLoadServerUri = null;

private Button mLoadButton;
private Button mSaveButton;

private String mString;

// other code

OnHandleFileListener mLoadFileListener = new OnHandleFileListener() {
@Override
public void handleFile(final String filePath) {
    Toast.makeText(UploadToServer.this, "Load: " + filePath, Toast.LENGTH_SHORT).show();
    mString = filePath;
}
};
于 2013-10-26T18:03:02.820 回答