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가 생깁니 까? 경로를 수정하려면 어떻게해야합니까? 미래에는 카메라로 사진을 찍어서 지금 사용하고있는 아이콘 대신 사용하고 싶습니다.
방금 내 문제를 발견했습니다. 파일을 볼 수 있도록 Manifest에서 사용 권한을 설정해야했습니다. – AAlferez