Java 将PDF加载到ImageView中的正确方法
我正在寻找将PDF文件加载到ImageView的正确方法。 我使用Android中的BitmapWorkerTask类。 我有一个按钮,可以选择要上传到ImageView的文件。当我点击这个文件时,这个过程就开始了。 我的问题是,我的PDF是完美加载3倍5。我不明白为什么前几次不起作用Java 将PDF加载到ImageView中的正确方法,java,android,pdf,Java,Android,Pdf,我正在寻找将PDF文件加载到ImageView的正确方法。 我使用Android中的BitmapWorkerTask类。 我有一个按钮,可以选择要上传到ImageView的文件。当我点击这个文件时,这个过程就开始了。 我的问题是,我的PDF是完美加载3倍5。我不明白为什么前几次不起作用 ImageView map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns
ImageView map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = (ImageView)findViewById(R.id.pdf);
}
/**
* Use this to load a pdf file from your assets and render it to a Bitmap.
*
* @param context
* current context.
* @param filePath
* of the pdf file in the assets.
* @return a bitmap.
*/
@Nullable
public static Bitmap renderToBitmap(Context context, String filePath) {
Bitmap bi = null;
InputStream inStream = null;
try {
inStream = new FileInputStream(filePath);
bi = renderToBitmap(context, inStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inStream.close();
} catch (IOException e) {
// do nothing because the stream has already been closed
}
}
return bi;
}
/**
* Use this to render a pdf file given as InputStream to a Bitmap.
*
* @param context
* current context.
* @param inStream
* the inputStream of the pdf file.
* @return a bitmap.
* @see https://github.com/jblough/Android-Pdf-Viewer-Library/
*/
@Nullable
public static Bitmap renderToBitmap(Context context, InputStream inStream) {
Bitmap bi = null;
try {
byte[] decode = IOUtils.toByteArray(inStream);
ByteBuffer buf = ByteBuffer.wrap(decode);
PDFPage mPdfPage = new PDFFile(buf).getPage(0);
float width = mPdfPage.getWidth();
float height = mPdfPage.getHeight();
RectF clip = null;
bi = mPdfPage.getImage((int) (width), (int) (height), clip, true,
true);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inStream.close();
} catch (IOException e) {
// do nothing because the stream has already been closed
}
}
return bi;
}
private void renderMap() {
String mapFilePath = Environment.getExternalStorageDirectory().toString()+"/Android/data/com.empower.data/"+mapFileName;
BitmapWorkerTask task = new BitmapWorkerTask(map, mapFilePath, MainActivity.this);
task.loadBitmap(R.id.pdf, map, mapFilePath);
map.setVisibility(View.VISIBLE);
map.invalidate();
}
还有我的BitmapWorkerTask.java
public class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
private String mapFilePath1;
private Context context;
private RelativeLayout loadingPanel;
public BitmapWorkerTask(ImageView imageView, String mapFilePath2, Context context) {
// Use a WeakReference to ensure the ImageView can be garbage collected
this.context = context;
mapFilePath1 = mapFilePath2;
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
Bitmap bm = MainActivity.renderToBitmap(context , mapFilePath1);
return bm;
}
@Override
protected void onPreExecute() {
loadingPanel = (RelativeLayout)((Activity) context).findViewById(R.id.loadingPanel);
loadingPanel.setVisibility(View.VISIBLE);
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(final Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
loadingPanel.setVisibility(View.GONE);
}
}, 500);
}
}
}
public void loadBitmap(int resId, ImageView imageView, String mapFilePath1) {
BitmapWorkerTask task = new BitmapWorkerTask(imageView, mapFilePath1, context);
task.execute(resId);
}
}
公共类BitmapWorkerTask扩展异步任务{
私有最终WeakReference imageViewReference;
私有整数数据=0;
私有字符串mapFilePath1;
私人语境;
专用相对负载面板;
公共BitmapWorkerTask(ImageView ImageView、字符串mapFilePath2、上下文){
//使用WeakReference确保可以对ImageView进行垃圾收集
this.context=上下文;
mapFilePath1=mapFilePath2;
imageViewReference=新的WeakReference(imageView);
}
//在背景中解码图像。
@凌驾
受保护位图doInBackground(整数…参数){
数据=参数[0];
位图bm=MainActivity.renderToBitmap(上下文,mapFilePath1);
返回bm;
}
@凌驾
受保护的void onPreExecute(){
loadingPanel=(RelativeLayout)((活动)上下文).findViewById(R.id.loadingPanel);
loadingPanel.setVisibility(View.VISIBLE);
}
//完成后,查看ImageView是否仍然存在并设置位图。
@凌驾
受保护的void onPostExecute(最终位图){
if(imageViewReference!=null&&bitmap!=null){
最终ImageView=imageViewReference.get();
如果(imageView!=null){
new Handler().postDelayed(new Runnable()){
@凌驾
公开募捐{
设置图像位图(位图);
loadingPanel.setVisibility(View.GONE);
}
}, 500);
}
}
}
public void loadBitmap(int-resId、ImageView-ImageView、String-mapFilePath1){
BitmapWorkerTask任务=新的BitmapWorkerTask(imageView,mapFilePath1,上下文);
任务执行(resId);
}
}
当它不工作时会发生什么。是否有任何错误例如,第一次加载时,我只有图像的1/4,第二次加载时只有图像的一半。最后,在3次加载后,图像完全加载。似乎inputstream或buffer存在问题,库在成功或失败时是否提供任何回调不幸的是,即使我查看logcat,我也看不到与这部分代码相关的任何内容。它不提供错误/警告。因为这段代码没有向logcat发布任何内容!易于理解的