安卓工作室(java):使用iText将图像从数组转换为PDF



当前,用户在片段中选择图像,并将其转换为具有字符串路径名的数组。我想把那个图像放在PDF上,但格式有问题。我正在尝试使用下面的代码来解决这个问题。目前,所有内容都会一直检查到光标。MoveToFirst((返回null。

for (int i = 0; i <= imgArray.size(); i++) {
Uri selectedImageUri = Uri.fromFile(new File(imgArray.get(i)));
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImageUri, filePathColumn, null, null, null);
cursor.moveToFirst(); //ERROR: NULL
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bmp = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
doc.add(image);
}

解决方案:我想明白了。这似乎对我有用!使用位图配置。

for(int i=0;i<imgArray.size((;i++({

Bitmap bmp = BitmapFactory.decodeFile(imgArray.get(i));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

Image image = Image.getInstance(stream.toByteArray());
doc.add(image);

}

最新更新