2014-02-13 2 views
0

이 간단한 응용 프로그램을 가지고 있고 내가하고 싶은 일은 버튼을 누르면 WebView를 열고 내 이미지로 사용자 정의 HTML 웹 사이트를 표시하려고합니다. 입술/당김 폴더에 아이콘)이미지가 포함 된 사용자 정의 html로 Webview 열기

이 내 웹보기는 모습입니다 :

public class WebViewActivity extends Activity { 

private WebView webView; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.webview); 

    webView = (WebView) findViewById(R.id.webView1); 
    webView.getSettings().setJavaScriptEnabled(true); 
    //webView.loadUrl("http://www.google.com"); 

    //image to byte[] 
    Bitmap bmBef = null; 
    Bitmap bmAft = null; 
     BitmapFactory.Options bmo = new BitmapFactory.Options(); 
     bmo.inPreferredConfig = Config.ARGB_8888; 

     String patth = "android.resource://com.mkyong.android/drawable/ic_launcher.png"; 
     bmBef = BitmapFactory.decodeFile(patth, bmo); 
     //byte[] b = bitmapToByteArray(bmBef); 





    byte[] imageRaw = bitmapToByteArray(bmBef); 
     String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT); 
    String customHtml = "<html><body><h1>Hello, WebView</h1><img src=\"data:image/jpeg;base64," + image64 + "\" /></img></body></html>"; 
    webView.loadData(customHtml, "text/html", "UTF-8"); 

} 

public static byte[] bitmapToByteArray(Bitmap bm) { 
    // Create the buffer with the correct size 
    int iBytes = bm.getWidth() * bm.getHeight() * 4; 
    ByteBuffer buffer = ByteBuffer.allocate(iBytes); 

    // Log.e("DBG", buffer.remaining()+""); -- Returns a correct number based on dimensions 
    // Copy to buffer and then into byte array 
    bm.copyPixelsToBuffer(buffer); 
    // Log.e("DBG", buffer.remaining()+""); -- Returns 0 
    return buffer.array(); 
} 

} 

그러나이 줄 내가 무엇을 놓치고 int iBytes = bm.getWidth() * bm.getHeight() * 4; 에 충돌? 경로가 잘못되어 그 경로에 이미지가 없으므로 nullpointer가 생깁니 까? 경로를 수정하려면 어떻게해야합니까? 미래에는 카메라로 사진을 찍어서 지금 사용하고있는 아이콘 대신 사용하고 싶습니다.

답변

0

오류가 발생하면 오류가 무엇인가요? LogCat 출력에서 ​​문제의 원인을 찾아보십시오. 한편,이 코드는 웹보기에 나를 위해 이미지를로드 :

File rootDirectory = mContext.getFilesDir(); 
String baseUrl = "file://" + rootDirectory.getAbsolutePath() + File.separator + filePath + File.separator;   

String imageName = <somthing_that_provides_your_fiename>; 
Bitmap image = BitmapFactory.decodeFile(rootDirectory.getAbsolutePath() + File.separator + filePath + imageName); 
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); 
Display display = windowManager.getDefaultDisplay(); 
int width = display.getWidth(); 

int scale = width * 100/image.getWidth(); 

browser.setInitialScale(scale); 
browser.loadUrl(baseUrl + imageName); 

나는 폭에 따라 이미지를 확장하고있어하지만 당신은 폭과 높이를 확인 할 수 있습니다.

+0

방금 ​​내 문제를 발견했습니다. 파일을 볼 수 있도록 Manifest에서 사용 권한을 설정해야했습니다. – AAlferez

관련 문제