PDF Viewerとブラウザを1画面で動作するアプリを作成しています。
画面上部にブラウザを出したいのですが、うまくブラウザが機能しません。
真っ白く表示してしまいます。
問題の原因が分からなくて困っています。
解決策のアドバイスが欲しいです。
なお、manifestには下記を追加しています。
<uses-permission android:name="android.permission.INTERNET"/>
よろしくお願い致します。
Activity.java
1import java.util.HashMap; 2import java.util.Map; 3 4import android.content.ActivityNotFoundException; 5import android.content.Intent; 6import android.content.pm.PackageManager; 7import android.database.Cursor; 8import android.graphics.Color; 9import android.net.Uri; 10import android.os.Bundle; 11import android.provider.OpenableColumns; 12import android.support.annotation.NonNull; 13import android.support.v4.app.ActivityCompat; 14import android.support.v4.content.ContextCompat; 15import android.support.v7.app.AppCompatActivity; 16import android.util.Log; 17import android.widget.Toast; 18 19import com.github.barteksc.pdfviewer.PDFView; 20import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener; 21import com.github.barteksc.pdfviewer.listener.OnPageChangeListener; 22import com.github.barteksc.pdfviewer.listener.OnPageErrorListener; 23import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle; 24import com.github.barteksc.pdfviewer.util.FitPolicy; 25import com.shockwave.pdfium.PdfDocument; 26 27import org.androidannotations.annotations.AfterViews; 28import org.androidannotations.annotations.EActivity; 29import org.androidannotations.annotations.NonConfigurationInstance; 30import org.androidannotations.annotations.OnActivityResult; 31import org.androidannotations.annotations.OptionsItem; 32import org.androidannotations.annotations.OptionsMenu; 33import org.androidannotations.annotations.ViewById; 34 35import java.util.List; 36 37 38import android.webkit.WebView; 39import android.webkit.WebViewClient; 40import android.view.KeyEvent; 41 42@EActivity(R.layout.activity_main) 43@OptionsMenu(R.menu.options) 44public class PDFViewActivity extends AppCompatActivity implements OnPageChangeListener, OnLoadCompleteListener, 45 OnPageErrorListener { 46 47 private static final String TAG = PDFViewActivity.class.getSimpleName(); 48 49 private final static int REQUEST_CODE = 42; 50 public static final int PERMISSION_CODE = 42042; 51 52 public static final String SAMPLE_FILE = "sample.pdf"; 53 public static final String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE"; 54 // 55 // 56 private WebView mainView = null; 57 58 // 59 60 @ViewById 61 PDFView pdfView; 62 63 @NonConfigurationInstance 64 Uri uri; 65 66 @NonConfigurationInstance 67 Integer pageNumber = 0; 68 69 String pdfFileName; 70 71 @OptionsItem(R.id.pickFile) 72 void pickFile() { 73 int permissionCheck = ContextCompat.checkSelfPermission(this, 74 READ_EXTERNAL_STORAGE); 75 76 if (permissionCheck != PackageManager.PERMISSION_GRANTED) { 77 ActivityCompat.requestPermissions( 78 this, 79 new String[]{READ_EXTERNAL_STORAGE}, 80 PERMISSION_CODE 81 ); 82 83 return; 84 } 85 86 launchPicker(); 87 } 88 89 void launchPicker() { 90 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 91 intent.setType("application/pdf"); 92 try { 93 startActivityForResult(intent, REQUEST_CODE); 94 } catch (ActivityNotFoundException e) { 95 //alert user that file manager not working 96 Toast.makeText(this, R.string.toast_pick_file_error, Toast.LENGTH_SHORT).show(); 97 } 98 } 99 100 @AfterViews 101 void afterViews() { 102 pdfView.setBackgroundColor(Color.LTGRAY); 103 if (uri != null) { 104 displayFromUri(uri); 105 } else { 106 displayFromAsset(SAMPLE_FILE); 107 } 108 setTitle(pdfFileName); 109 } 110 111 private void displayFromAsset(String assetFileName) { 112 pdfFileName = assetFileName; 113 114 pdfView.fromAsset(SAMPLE_FILE) 115 .defaultPage(pageNumber) 116 .onPageChange(this) 117 .enableAnnotationRendering(true) 118 .onLoad(this) 119 .scrollHandle(new DefaultScrollHandle(this)) 120 .spacing(10) // in dp 121 .onPageError(this) 122 .pageFitPolicy(FitPolicy.BOTH) 123 .load(); 124 } 125 126 private void displayFromUri(Uri uri) { 127 pdfFileName = getFileName(uri); 128 129 pdfView.fromUri(uri) 130 .defaultPage(pageNumber) 131 .onPageChange(this) 132 .enableAnnotationRendering(true) 133 .onLoad(this) 134 .scrollHandle(new DefaultScrollHandle(this)) 135 .spacing(10) // in dp 136 .onPageError(this) 137 .load(); 138 } 139 140 @OnActivityResult(REQUEST_CODE) 141 public void onResult(int resultCode, Intent intent) { 142 if (resultCode == RESULT_OK) { 143 uri = intent.getData(); 144 displayFromUri(uri); 145 } 146 } 147 148 @Override 149 public void onPageChanged(int page, int pageCount) { 150 pageNumber = page; 151 setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount)); 152 } 153 154 public String getFileName(Uri uri) { 155 String result = null; 156 if (uri.getScheme().equals("content")) { 157 Cursor cursor = getContentResolver().query(uri, null, null, null, null); 158 try { 159 if (cursor != null && cursor.moveToFirst()) { 160 result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); 161 } 162 } finally { 163 if (cursor != null) { 164 cursor.close(); 165 } 166 } 167 } 168 if (result == null) { 169 result = uri.getLastPathSegment(); 170 } 171 return result; 172 } 173 174 @Override 175 public void loadComplete(int nbPages) { 176 PdfDocument.Meta meta = pdfView.getDocumentMeta(); 177 Log.e(TAG, "title = " + meta.getTitle()); 178 Log.e(TAG, "author = " + meta.getAuthor()); 179 Log.e(TAG, "subject = " + meta.getSubject()); 180 Log.e(TAG, "keywords = " + meta.getKeywords()); 181 Log.e(TAG, "creator = " + meta.getCreator()); 182 Log.e(TAG, "producer = " + meta.getProducer()); 183 Log.e(TAG, "creationDate = " + meta.getCreationDate()); 184 Log.e(TAG, "modDate = " + meta.getModDate()); 185 186 printBookmarksTree(pdfView.getTableOfContents(), "-"); 187 188 } 189 190 public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) { 191 for (PdfDocument.Bookmark b : tree) { 192 193 Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx())); 194 195 if (b.hasChildren()) { 196 printBookmarksTree(b.getChildren(), sep + "-"); 197 } 198 } 199 } 200 201 /** 202 * Listener for response to user permission request 203 * 204 * @param requestCode Check that permission request code matches 205 * @param permissions Permissions that requested 206 * @param grantResults Whether permissions granted 207 */ 208 @Override 209 public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], 210 @NonNull int[] grantResults) { 211 if (requestCode == PERMISSION_CODE) { 212 if (grantResults.length > 0 213 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 214 launchPicker(); 215 } 216 } 217 } 218 219 @Override 220 public void onPageError(int page, Throwable t) { 221 Log.e(TAG, "Cannot load page " + page); 222 } 223// 224 225//ブラウザ部分 226 @Override 227 protected void onCreate(Bundle savedInstanceState) { 228 super.onCreate(savedInstanceState); 229 setContentView(R.layout.activity_main); 230 231 //コントロール取得 232 this.mainView = (WebView) findViewById(R.id.webView); 233 234 mainView.setWebViewClient(new WebViewClient()); 235 //表示URL設定 236 this.mainView.loadUrl("https://google.co.jp/"); 237 238 } 239 240 public boolean onKeyDown(int keyCode, KeyEvent event) { 241 // 端末の戻るボタンでブラウザバック 242 if (keyCode == KeyEvent.KEYCODE_BACK && mainView.canGoBack()) { 243 mainView.goBack(); 244 return true; 245 } 246 return super.onKeyDown(keyCode, event); 247 } 248 249 250} 251
あなたの回答
tips
プレビュー