Accessing phones storage in an app developed using webview

Viewed 174

I have converted a website to an app using webview where I need to access the images and files from the phone's storage. I tried adding READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE but it doesn't work.button to access the storage is browse

here is my MainActivity.java code

import androidx.appcompat.app.AppCompatActivity;                      
import android.os.Bundle;                                                
import android.webkit.WebSettings;                                      
import android.webkit.WebView;                                        
import android.webkit.WebViewClient;                                       
                                                                               
public class MainActivity extends AppCompatActivity {                          
    private WebView mywebview;                                                 
                                                                               
    @Override                                                                  
    protected void onCreate(Bundle savedInstanceState) {                       
        super.onCreate(savedInstanceState);                                    
        setContentView(R.layout.activity_main);                                
        mywebview = (WebView) findViewById(R.id.webview);                      
        WebSettings webSettings = mywebview.getSettings();                     
        webSettings.setJavaScriptEnabled(true);                                
        mywebview.loadUrl("https://www.google.com");                           
        mywebview.setWebViewClient(new WebViewClient());                       
    }                                                                          
                                                                               
    @Override                                                                  
    public void onBackPressed() {                                            
        if (mywebview.canGoBack()) {                                            
            mywebview.goBack();                                                
        } else {                                                               
            super.onBackPressed();                                             
        }                                                                      
    }                                                                          
}

AndroidManifest.xml file is here

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

kindly suggest a solution.

1 Answers

You should consider doing a permission check at start in oncreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    isStoragePermissionGranted();
    ...
}

public  boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        return true;
    } else {

        Log.v(TAG,"Permission is revoked");
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        return false;
    }
}
else { //permission is automatically granted on sdk<23 upon installation
    Log.v(TAG,"Permission is granted");
    return true;
}
}

    @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
        //resume tasks needing this permission
    }
    }
Related