在textview中显示文件路径

时间:2017-02-27 09:32:51

标签: android server filepath android-external-storage

我的应用程序访问手机的外部存储并选择特定文件并在textview中显示路径,然后将文件发送到服务器。我做了第一部分我从外部存储选择文件。如何显示其路径并将文件发送到服务器? 那是我的代码

public class MainActivity extends AppCompatActivity {
private static final int FILE_SELECT_CODE = 0;
//private static final int REQUEST_CODE = 6384;
private static final String TAG = "MainActivity";
TextView add;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button go=(Button)findViewById(R.id.goo);

    go.setOnClickListener(
            new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    goo();



                }
            }


    );
}
public  void  goo(){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                FILE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Please install a File Manager.",
                Toast.LENGTH_SHORT).show();
    }

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {
                // Get the Uri of the selected file
                Uri uri = data.getData();
                Log.d(TAG, "File Uri: " + uri.toString());
                // Get the path
                add=(TextView)findViewById(R.id.address);

                String path = null;
                try {
                    path = FileUtils.getPath(MainActivity.this, uri);
                    //add.setText(path);

                } catch (URISyntaxException e) {
                    e.printStackTrace();
                    Toast.makeText(this,"wrong happened",Toast.LENGTH_LONG).show();
                }
                Log.d(TAG, "File Path: " + path);
                // Get the file instance
                // File file = new File(path);
                // Initiate the upload

            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
} 

文件实用程序

public class FileUtils {

public static String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

}

0 个答案:

没有答案