Skip to content

Commit 0ebcba7

Browse files
committed
fix: crash with zero page count while loading failed
1 parent 077d085 commit 0ebcba7

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

pdfview/src/main/java/com/zjy/pdfview/PdfView.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.zjy.pdfview;
22

3+
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
4+
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
5+
import static com.zjy.pdfview.constants.Constants.DOWNLOAD_ACTION;
6+
import static com.zjy.pdfview.download.DownloadService.DOWNLOAD_URL_KEY;
7+
38
import android.content.Context;
49
import android.content.Intent;
510
import android.content.IntentFilter;
@@ -12,15 +17,12 @@
1217
import android.text.TextUtils;
1318
import android.util.AttributeSet;
1419
import android.view.LayoutInflater;
15-
import android.view.View;
1620
import android.view.ViewGroup;
1721
import android.widget.FrameLayout;
1822

1923
import androidx.annotation.NonNull;
2024
import androidx.annotation.Nullable;
2125
import androidx.annotation.RequiresApi;
22-
import androidx.constraintlayout.widget.ConstraintLayout;
23-
import androidx.constraintlayout.widget.ConstraintSet;
2426
import androidx.recyclerview.widget.RecyclerView;
2527

2628
import com.zjy.pdfview.adapter.PdfPageAdapter;
@@ -41,11 +43,6 @@
4143
import java.util.ArrayList;
4244
import java.util.List;
4345

44-
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
45-
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
46-
import static com.zjy.pdfview.constants.Constants.DOWNLOAD_ACTION;
47-
import static com.zjy.pdfview.download.DownloadService.DOWNLOAD_URL_KEY;
48-
4946
/**
5047
* Date: 2021/1/27
5148
* Author: Yang
@@ -144,12 +141,9 @@ public void onPageSelected(int position, boolean isBottom) {
144141
});
145142
contentRv.setLayoutManager(pageLayoutManager);
146143

147-
loadingLayout.setLoadLayoutListener(new PdfLoadingLayout.LoadLayoutListener() {
148-
@Override
149-
public void clickRetry() {
150-
if (!TextUtils.isEmpty(pdfUrl)) {
151-
loadPdf(pdfUrl);
152-
}
144+
loadingLayout.setLoadLayoutListener(() -> {
145+
if (!TextUtils.isEmpty(pdfUrl)) {
146+
loadPdf(pdfUrl);
153147
}
154148
});
155149

@@ -159,19 +153,17 @@ public void clickRetry() {
159153

160154
getOperateView().addOperateListener(this);
161155

162-
scrollSlider.setScrollSlideListener(new ScrollSlider.ScrollSlideListener() {
163-
@Override
164-
public boolean scrolling(int scrollY) {
165-
int pageItemHeight = contentRv.getHeight() / pageCount;
166-
int scrollIndex = (int) scrollY / pageItemHeight;
167-
if(scrollIndex >= 0 && scrollIndex < pageLayoutManager.getItemCount()) {
168-
scrollSlider.setTranslationY(scrollY - scrollY % pageItemHeight);
169-
currentIndex = scrollIndex;
170-
pageLayoutManager.scrollToPosition(currentIndex);
171-
getOperateView().setPageIndexText(generatePageIndexText());
172-
}
173-
return true;
156+
scrollSlider.setScrollSlideListener(scrollY -> {
157+
if (pageCount == 0) return true;
158+
int pageItemHeight = contentRv.getHeight() / pageCount;
159+
int scrollIndex = scrollY / pageItemHeight;
160+
if(scrollIndex >= 0 && scrollIndex < pageLayoutManager.getItemCount()) {
161+
scrollSlider.setTranslationY(scrollY - scrollY % pageItemHeight);
162+
currentIndex = scrollIndex;
163+
pageLayoutManager.scrollToPosition(currentIndex);
164+
getOperateView().setPageIndexText(generatePageIndexText());
174165
}
166+
return true;
175167
});
176168
}
177169

@@ -342,6 +334,8 @@ protected Boolean doInBackground(Void... voids) {
342334
@Override
343335
protected void onPostExecute(Boolean result) {
344336
super.onPostExecute(result);
337+
scrollSlider.setVisibility(pageCount == 0 ? GONE : VISIBLE);
338+
345339
if (result) {
346340
getOperateView().setPageIndexText(generatePageIndexText());
347341
pageAdapter.notifyDataSetChanged();

pdfview/src/main/java/com/zjy/pdfview/adapter/PdfPageAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
2727
public class PdfPageAdapter extends RecyclerView.Adapter<PdfPageAdapter.ViewHolder> {
2828

29-
private Context context;
30-
private List<Bitmap> pageList;
29+
final private Context context;
30+
final private List<Bitmap> pageList;
3131

3232
public PdfPageAdapter(Context context, List<Bitmap> pageList) {
3333
this.context = context;

pdfview/src/main/java/com/zjy/pdfview/utils/FileUtils.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ public static File writeAssetsToFile(Context context, String assetPath) throws I
1818
String fileName = assetPath.substring(assetPath.lastIndexOf("/") + 1);
1919
File resultFile = checkCacheSize(context, assetPath);
2020
if (!resultFile.exists()) {
21-
resultFile.createNewFile();
21+
boolean createResult = resultFile.createNewFile();
22+
if (!createResult) {
23+
return null;
24+
}
2225
}
2326
InputStream is = context.getAssets().open(fileName);
2427
FileOutputStream fos = null;
2528
try {
2629
byte[] data = new byte[2048];
27-
int nbread = 0;
30+
int readBuffer;
2831
fos = new FileOutputStream(resultFile);
29-
while ((nbread = is.read(data)) > -1) {
30-
fos.write(data, 0, nbread);
32+
while ((readBuffer = is.read(data)) > -1) {
33+
fos.write(data, 0, readBuffer);
3134
}
3235
} catch (Exception ex) {
3336
PdfLog.logError("Exception: " + ex);
@@ -75,8 +78,7 @@ private static File checkCacheSize(Context context, String url) {
7578
}
7679
File[] cacheList = folder.listFiles();
7780
if (cacheList != null && cacheList.length >= 10) {
78-
for (int i = 0; i < cacheList.length; i++) {
79-
File childFile = cacheList[i];
81+
for (File childFile : cacheList) {
8082
childFile.delete();
8183
}
8284
}

0 commit comments

Comments
 (0)