Android Studio (java):使用 iText 从数组到 PDF 的图像

目前,用户在片段中选择他们的图像,并将它们转换为带有字符串路径名的数组。我想将该图像放在 PDF 上,但存在格式问题。我正在尝试使用下面的代码来解决这个问题。目前一切检查通过,直到 cursor.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);


        }


皈依舞
浏览 190回答 1
1回答

泛舟湖上清波郎朗

解决方案:我想出了这个。这似乎对我有用!使用位图配置。for (int i = 0; i < imgArray.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bitmap bmp = BitmapFactory.decodeFile(imgArray.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream stream = new ByteArrayOutputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Image image = Image.getInstance(stream.toByteArray());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc.add(image);&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java