WebView + ContentProvider

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

WebView + ContentProvider

Postby vadimz » Fri Sep 25, 2009 1:11 pm

I need community help :)

I write program that extracts webpage in local application files and opens them with WebView.
For to get access for browser to local files I wrote own content provider with openFile() override.

But after that browser cannot open image, it only can open HTML, instead of image there is blue "?".

Extract and open page code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void onCreate(Bundle savedInstanceState) {
  2.                 super.onCreate(savedInstanceState);
  3.                 setContentView(R.layout.main);
  4.                
  5.                 try {
  6.                         InputStream ins = getResources().openRawResource(R.raw.book);
  7.                         ZipInputStream zip = new ZipInputStream(ins);
  8.                                                
  9.                         ZipEntry ze;                   
  10.                         while ((ze = zip.getNextEntry()) != null)
  11.                         {
  12.                                 int size = (int)ze.getSize();
  13.                                 byte buffer[] = new byte[size];
  14.                                 zip.read(buffer, 0, (int)ze.getSize());
  15.                                 FileOutputStream wri = this.openFileOutput(ze.getName(), MODE_PRIVATE);
  16.                                 wri.write(buffer);
  17.                                 wri.close();                                   
  18.                         }
  19.                 } catch (IOException e) {};
  20.  
  21.                 WebView browser = (WebView) findViewById(R.id.browser);
  22.                 //browser.loadUrl("content://com.topbytelabs.book1/index.htm");        
  23.                 browser.loadUrl("content://com.topbytelabs.book1/1.jpg");
  24.         }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Content provider:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.topbytelabs.book1;
  2.  
  3. import java.io.*;
  4.  
  5. import android.content.*;
  6. import android.database.*;
  7. import android.net.*;
  8. import android.os.*;
  9.  
  10. public class LocalFileContentProvider extends ContentProvider {
  11.    private static final String URI_PREFIX = "content://com.topbytelabs.book1";
  12.  
  13.    public static String constructUri(String url) {
  14.        Uri uri = Uri.parse(url);
  15.        return uri.isAbsolute() ? url : URI_PREFIX + url;
  16.    }
  17.  
  18.    @Override
  19.    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
  20.                    
  21.        File file = new File(getContext().getFilesDir() + uri.getPath());      
  22.        ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
  23.        return parcel;
  24.    }
  25.  
  26.    @Override
  27.    public boolean onCreate() {
  28.        return true;
  29.    }
  30.  
  31.    @Override
  32.    public int delete(Uri uri, String s, String[] as) {
  33.        throw new UnsupportedOperationException("Not supported by this provider");
  34.    }
  35.  
  36.    @Override
  37.    public String getType(Uri uri) {
  38.        throw new UnsupportedOperationException("Not supported by this provider");
  39.    }
  40.  
  41.    @Override
  42.    public Uri insert(Uri uri, ContentValues contentvalues) {
  43.        throw new UnsupportedOperationException("Not supported by this provider");
  44.    }
  45.  
  46.    @Override
  47.    public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
  48.        throw new UnsupportedOperationException("Not supported by this provider");
  49.    }
  50.  
  51.    @Override
  52.    public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
  53.        throw new UnsupportedOperationException("Not supported by this provider");
  54.    }
  55.  
  56. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Manifest tag:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <provider android:name=".LocalFileContentProvider"
  2.          android:authorities="com.topbytelabs.book1" />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


PS
I tested with txt files(ok), png files(fail).
It seems it corruptes binary formats.
vadimz
Developer
Developer
 
Posts: 44
Joined: Thu Jul 30, 2009 5:04 pm
Location: Moldova

Top

Postby Ingersol » Sat Sep 26, 2009 3:25 am

Wrong unzipping code. Close this topic.
Ingersol
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sat Dec 06, 2008 2:49 pm

Postby vadimz » Sat Sep 26, 2009 3:24 pm

Ingersol wrote:Wrong unzipping code. Close this topic.


Thanks. No ability to close, just consider it to be CLOSED.
vadimz
Developer
Developer
 
Posts: 44
Joined: Thu Jul 30, 2009 5:04 pm
Location: Moldova

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests