• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to allow file upload and download in webview?

Technologx

Newbie
How do I allow users to upload and download files to my website from my app?

Here's my code:
Code:
package technologx.technologx;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.Window;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Adds Progress Bar Support
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        // Makes Progress Bar Visible
        getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

        // Use forum.xml as webview layout
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);

        // Adds Zoom Control (You may not need this)
        webView.getSettings().setSupportZoom(true);

        // Enables Multi-Touch. if supported by ROM
        webView.getSettings().setBuiltInZoomControls(true);

        // Change to your own forum url
        webView.loadUrl("http://technologx.fulba.com/");

        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // Loads only your forum domain and no others!
                if(url.contains("technologx.fulba.com") == true) {
                    view.loadUrl(url);
                    // Adds Progress Bar Support
                    super.onPageStarted(view, url, null);
                    findViewById(R.id.progressbar).setVisibility(View.VISIBLE);
                    // If they are not your domain, use browser instead
                } else {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(i);
                }
                return true;
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                // Removes Progress Bar
                findViewById(R.id.progressbar).setVisibility(View.GONE);
                // Adds Cookies. Yummy!
                CookieSyncManager.getInstance().sync();
            }
        });
    }
    @Override
    public void onBackPressed() {
        // Enables going back history
        if (webView.copyBackForwardList().getCurrentIndex() > 0) {
            webView.goBack();
        }
        else {
            // Your exit alert code, or alternatively line below to finish
            // Finishes forum activity
            super.onBackPressed();
        }
    }
}
 
What technology are you using to implement your web page?
I could tell you how to do it using the Wicket web framework, but that probably wouldn't be much value to you.
 
Code:
mWebView.setDownloadListener(new DownloadListener() {
                public void onDownloadStart(String url, String userAgent,
                        String contentDisposition, String mimetype,
                        long contentLength) {
         Request request = new Request(Uri.parse(url));
                            request.allowScanningByMediaScanner();
                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                          
                          
                            String newString ="THE FILE NAME.mp3"
                          
                          
                        
                            Log.i(ERROR, "this is it " + newString);
                            File sdCard = Environment.getExternalStorageDirectory();
                            String folder = sdCard.getAbsolutePath() ;
                        
                         
                           request.setDestinationInExternalPublicDir(folder, newString);
                          
                         
                            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                          
                          
                            dm.enqueue(request);
                          
                          

                }
            });
 
Last edited:
Now th
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);


String newString ="THE FILE NAME.mp3"



Log.i(ERROR, "this is it " + newString);
File sdCard = Environment.getExternalStorageDirectory();
String folder = sdCard.getAbsolutePath() ;


request.setDestinationInExternalPublicDir(folder, newString);


DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);


dm.enqueue(request);



}
});
e file name will always be THE FILE NAME.mp3

you could do something like this to replace the string that it gets from the URL
String newString = url.replace("http://somewebsite.com/", "");
String newString1 = newString.replace("%20", " ");
String newString2 = newString1.replace("/filelocation/", "");

and then change
request.setDestinationInExternalPublicDir(folder, newString2);
I would use log to trace out the file getting sent back to you
 
I would find where to determine the file type.
Code:
String string ="Test, I am Adam.mp3";
// Anywhere in string b = string.indexOf(".mp3")>0;
// true if contains
// Anywhere in string b = string.matches("(?.mp3).*.mp3.*");
// true if contains but ignore case
// Anywhere in string b = string.contains(".mp3");
 
Last edited:
if the url is submitting the string back to download as follows as an example
Code:
//http://www.mycell.rocks/temp/Nickelback%20Far%20Away.mp3

I would do this
// String replace mycell.rocks address with nothing
String newString = url.replace("http://www.mycell.rocks/", "");
//String replace %20 with a empty space
String newString1 = newString.replace("%20", " ");
//String replace the temp folder with nothing
String newString2 = newString1.replace("/temp/", "");

//now if I Log new string to see what returns back to me as a name I should see

//Nickelback Far Away.mp3

//so say the I need to know what type of file what could I do?

//I could add some condition

String FiletypeMp3 = newString2.contains(".mp3");
String FiletypeJpg = newString2.contains(".jpg");

if(FiletypeMp3)
{
//do something
}

if(FiletypeJpg)
{
//do something
}


/*and if that dose not work I could read the last 4 letters of the string
now I'm not real sure but I think it might crash with a null pointer exception doing it
this way  it might be best to read the last 4 to get the extension type
because if the string returns and .mp3 and not .jpg the jpg string might end up being null not tested just my ideas on it*/

//and if that is the case I would do this

String newString3 = substring(newString2.length()-4));
//now my newString3 should say just the last 4 characters and I can do a condition this way


if(newString3 == ".mp3")
{
//do something
}
if(newString3 == ".jpg")
{
//do something
}
if(newString3 == "mpe3")
{
//do something
}
if(newString3 == "jpeg")
{
//do something
}
if(newString3 == ".png")
{
//do something
}
if(newString3 == ".gif")
{
//do something
}
if(newString3 == ".zip")
{
//do something
}
if(newString3 == ".rar")
{
//do something
}
if(newString3 == ".exe")
{
//do something
}
if(newString3 == ".pfd")
{
//do something
}
 
Last edited:
if the url is submitting the string back to download as follows as an example
Code:
//http://www.mycell.rocks/temp/Nickelback%20Far%20Away.mp3

I would do this
// String replace mycell.rocks address with nothing
String newString = url.replace("http://www.mycell.rocks/", "");
//String replace %20 with a empty space
String newString1 = newString.replace("%20", " ");
//String replace the temp folder with nothing
String newString2 = newString1.replace("/temp/", "");

//now if I Log new string to see what returns back to me as a name I should see

//Nickelback Far Away.mp3

//so say the I need to know what type of file what could I do?

//I could add some condition

String FiletypeMp3 = newString2.contains(".mp3");
String FiletypeJpg = newString2.contains(".jpg");

if(FiletypeMp3)
{
//do something
}

if(FiletypeJpg)
{
//do something
}


/*and if that dose not work I could read the last 4 letters of the string
now I'm not real sure but I think it might crash with a null pointer exception doing it
this way  it might be best to read the last 4 to get the extension type
because if the string returns and .mp3 and not .jpg the jpg string might end up being null not tested just my ideas on it*/

//and if that is the case I would do this

String newString3 = substring(newString2.length()-4));
//now my newString3 should say just the last 4 characters and I can do a condition this way


if(newString3 == ".mp3")
{
//do something
}
if(newString3 == ".jpg")
{
//do something
}
if(newString3 == "mpe3")
{
//do something
}
if(newString3 == "jpeg")
{
//do something
}
if(newString3 == ".png")
{
//do something
}
if(newString3 == ".gif")
{
//do something
}
if(newString3 == ".zip")
{
//do something
}
if(newString3 == ".rar")
{
//do something
}
if(newString3 == ".exe")
{
//do something
}
if(newString3 == ".pfd")
{
//do something
}

So I'd have to do a line for each file extension there is on my website? Also what code would I need to add in the do something area to save the files to the download directory on the SDCard?
 
Back
Top Bottom