P
P
phoenix1242018-07-16 10:59:17
Android
phoenix124, 2018-07-16 10:59:17

How to solve error with following hyperlinks in WebView?

There is a simple WebView that opens a text document. Code below if needed

public class ValkyriaViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stigmata_view);
        WebView webView = findViewById(R.id.webView);
       webView.setBackgroundColor(Color.parseColor("#CCCCCC"));
        Intent intent = getIntent();
        //получаем строку и формируем имя ресурса
        String resName = "v" + intent.getIntExtra("mValkyrieName", 0) + "s" + intent.getIntExtra("mSuitName", 0);
        Log.i("name", resName);
        Context context = getBaseContext(); 

       
        String text = readRawTextFile(context, getResources().getIdentifier(resName,
                "raw", "хххх"));

        webView.loadDataWithBaseURL("file:///android_res/raw/",text,"txt/html","UTF-8",null);
    }

    
    private String readRawTextFile(Context context, int resId)
    {
        InputStream inputStream = context.getResources().openRawResource(resId);

        InputStreamReader inputReader = new InputStreamReader(inputStream);
        BufferedReader buffReader = new BufferedReader(inputReader);
        String line;
        StringBuilder builder = new StringBuilder();

        try {
            while (( line = buffReader.readLine()) != null) {
                builder.append(line);
                builder.append("\n");
            }
        } catch (IOException e) {
            return null;
        }
        return builder.toString();
    }
}

Code in a text document
<a href="file:///android_res/raw/open/ххх">........</a>

I want to be able to open another in one text document, here is the error that comes out in Android Studio and the application crashes
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ххх, PID: 27080
                  android.os.FileUriExposedException: file:///android_res/raw/v0s1.txt exposed beyond app through Intent.getData()

I found a solution on the Internet using provider_patchs, but it seems to me that it will not help me, because the text file is opened, roughly speaking, not by the studio, but by another text document.
Is there any way to get around this error?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question