我的代码大部分工作正常。我不知道如何将文件预览位图图像传递给构造函数。如何将位图图像设置为自定义ListView
适配器的图像视图?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_every_day__file_explorer);
fileNameList=(ArrayList<File_Explorer>)getLastNonConfigurationInstance();
if(fileNameList==null) {
ListView lv = (ListView)findViewById(R.id.listView);
lv.setItemsCanFocus(false);
lv.setChoiceMode(lv.CHOICE_MODE_MULTIPLE);
fileNameList = new ArrayList<File_Explorer>();
arrayAdapter = new ArrayAdapter<File_Explorer>(this,
android.R.layout.simple_list_item_1, fileNameList);
lv.setAdapter(arrayAdapter);
File file =new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"Every_Day");
list = file.listFiles();
byte[] bytes;
try {
FileInputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
ByteBuffer buffer = ByteBuffer.NEW(bytes);
String data = Base64.encodeToString(bytes, Base64.DEFAULT);
PDFFile pdf_file = new PDFFile(buffer);
PDFPage page = pdf_file.getPage(2, true);
RectF rect = new RectF(0, 0, (int) page.getBBox().width(),
(int) page.getBBox().height());
Bitmap image = page.getImage((int)rect.width(), (int)rect.height(), rect);
FileOutputStream os = new FileOutputStream("/storage/extSdCard/pdf.jpg");
image.compress(Bitmap.CompressFormat.PNG, 80, os);
ImageView i = ((ImageView) findViewById(R.id.fileimage));
BitmapDrawable ob = new BitmapDrawable(getResources(), image);
i.setBackgroundDrawable(ob);
if(list!=null) {
for (int i = 0; i < list.length; i++) {
// *** problem below -- how to pass the image to File_Explorer?
fileNameList.add(new File_Explorer(list[i].getName(),???));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
arrayList = new ArrayList<File_Explorer>();
arrayList.addAll(fileNameList);
arrayAdapter = new FileExplorerArrayAdapter(this,arrayList);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String file = list[(int)id].getName();
openPdfIntent(file);
}
});
}
}
}