1

I have a filechooser that I call in a webview in order to upload a file. The method allowing me to retrieve the file in my filechooser activity is as follows :

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Option o = adapter.getItem(position);
    if (o.isFolder() || o.isParent()) {
        currentDir = new File(o.getPath());
        fill(currentDir);
    } else {
        //onFileClick(o);
        fileSelected = new File(o.getPath());
        Intent intent = new Intent();
        intent.putExtra("fileSelected", fileSelected.getAbsolutePath());
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}

This only allows me to get the path in the onActivityResult of my webview (which calls my filechooser to upload file). The method onActivityResult is given by the code below.

If I use another application installed in my phone other than my filechooser I use :

Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();

then the result is sent by:

 this.mUploadMessage.onReceiveValue(uri);

Since then, it works normally. But with my filechooser intent.getData () equals to null.

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == this.mUploadMessage) {
                return;
            }
            if (resultCode != RESULT_OK) {
                mUploadMessage.onReceiveValue(null);
                var = 0;
                return;
            }
           // Toast.makeText(getApplicationContext() , "onActivityResult()" + String.valueOf(requestCode) ,Toast.LENGTH_LONG).show();
           // Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();


           // this.mUploadMessage = null;
            String fileSelected = intent.getStringExtra("fileSelected");
            Bundle result = intent.getExtras();
            //result =   Uri.parse(fileSelected);

            //this.mUploadMessage.onReceiveValue(result);

            if (resultCode == RESULT_OK) {
                if (intent != null) {
                    // Get the URI of the selected file
                    final Uri uri = intent.getData();
                    Log.i("TOTOTOT", "Uri = " + uri.toString());
                    Toast.makeText(this, fileSelected + "   " + uri.toString() , Toast.LENGTH_SHORT).show();
                    this.mUploadMessage.onReceiveValue(uri);
                }
            }
           super.onActivityResult(requestCode, resultCode, intent);

        }
    }

What should I return in my two methods in order to retrieve both data and path, not only the path of the sent file ? Should it be all the bundle or do you know how to get Data while using Intent.getData()?

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
zied
  • 201
  • 3
  • 7
  • 17

2 Answers2

1

What is need to get data from directory selected inside onActivityResult()
You got path selected so you can read file from there which you want ..

Use this method to read file as a string

private String readURLFromPath(File filePath){
    String dataString = "";


    // i have kept text.txt in the sd-card
    if (file.exists()) // check if file exist
    {
        // Read text from file
        StringBuilder text = new StringBuilder();

        try {
            BufferedReader br = new BufferedReader(new FileReader(filePath));
            String line;
            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        } catch (IOException e){
            // You'll need to add proper error handling here
        }
        // Set the text
        dataString = text.toString();
    }
    return dataString ;
}

this method will return data as a string now you can send this string data on server...

Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
  • I send this result using a webview to a server, when I check in server I have only the path and I haven't the file. – zied Apr 17 '14 at 09:30
  • inside onActivityResult() when u get path read the file first and store in any string and send to server , file data will go to server not file.. Hope u ll understand – Neha Shukla Apr 17 '14 at 09:35
  • can you explain with an example please. – zied Apr 17 '14 at 09:45
  • I tried this final Uri uri = Uri.fromFile(new File(fileSelected)) ; but I have the some problem – zied Apr 17 '14 at 09:53
0

the solution is to Get content uri from file path in android as mentioned Get content uri from file path

Community
  • 1
  • 1
zied
  • 201
  • 3
  • 7
  • 17