0

My app access external storage of phone and select specific file and show the path in textview then send the file to server . i did first part i selected file from external storage .now how to show its path and send file to server? that is my code

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);
} 

FileUtils

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;
}

}

Shorouk Adel
  • 127
  • 3
  • 20
  • see this http://stackoverflow.com/questions/20324155/get-filepath-and-filename-of-selected-gallery-image-in-android – Vishal Thakkar Feb 27 '17 at 09:55
  • **how to show its path and send file to server**. It depends on your server using which method to upload image (form-data format, json format, ...) using POST/PUT, ... You should check with api on server. – RoShan Shan Feb 27 '17 at 10:01
  • thanks , but i want to show path in textview how to do that with my code? – Shorouk Adel Feb 27 '17 at 10:15

0 Answers0