我要创建客户发票 然后,发票将包含每个商品的照片及其价格 每张发票可以包含客户购买的几件商品 所以每张账单可以有多张图片 价格和图像信息从网站获取并存储在列表中 当我想获取每张照片的地址并将其插入循环中带有以下代码的文件时,程序崩溃 如果我在循环外多次执行此程序,则该程序将正常运行
此代码对于资产文件夹中的一张图片正常工作
try {
InputStream inputStream = getContext().getAssets().open("logo1.png");
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image signature;
signature = Image.getInstance(stream.toByteArray());
signature.setAbsolutePosition(400f, 150f);
signature.scalePercent(100f);
signature.scaleAbsoluteHeight(50);
signature.scaleAbsoluteWidth((signature.getWidth() * 50) / signature.getHeight());
c1 = new PdfPCell(new Phrase(getResources().getString(R.string.total_price)));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(signature);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
}
catch(IOException ex)
{
ex.printStackTrace();
}
但是以下面类似的方式,来自网络的图像代码不起作用
Image imageFromWeb;
if(orderProductsList.size()>0) {
for (int i = 0; i < orderProductsList.size(); i++) {
String img = ConstantValues.ECOMMERCE_URL + orderProductsList.get(i).getImage();
try {
imageFromWeb = Image.getInstance(new URL(img));
imageFromWeb.setAbsolutePosition(400f, 150f);
imageFromWeb.scalePercent(100f);
imageFromWeb.scaleAbsoluteHeight(50);
imageFromWeb.scaleAbsoluteWidth((imageFromWeb.getWidth() * 50) / imageFromWeb.getHeight());
c1 = new PdfPCell(new Phrase(getResources().getString(R.string.total_price)));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(imageFromWeb);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
这是我的主要代码
public void GenerateInvoicePdf() {
path = Environment.getExternalStorageDirectory().toString()
+ "/PDF/" + "Name.pdf";
Document document = new Document(PageSize.LETTER);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/PDF");
myDir.mkdirs();
try {
PdfWriter.getInstance(document, new FileOutputStream(path));
document.open();
addMetaData(document);
addTitlePage(document);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
document.close();
Toast.makeText(getContext(), "PDF File is Created. Location : " + path,
Toast.LENGTH_LONG).show();
}
public void addMetaData(Document document)
{
document.addTitle("RESUME");
document.addSubject("Person Info");
document.addKeywords("Personal, Education, Skills");
document.addAuthor("TAG");
document.addCreator("TAG");
}
public void addTitlePage(Document document) throws DocumentException {
Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD
| Font.UNDERLINE, BaseColor.GRAY);
Paragraph prHead = new Paragraph();
prHead.setFont(titleFont);
prHead.add(DateFormat.getDateTimeInstance().format(new Date())+"\n");
prHead.add("\n Order Invoice \n\n");
prHead.setFont(catFont);
prHead.setAlignment(Element.ALIGN_CENTER);
document.add(prHead);
PdfPTable table = new PdfPTable(2);
PdfPCell c1 = new PdfPCell(new Phrase(getResources().getString(R.string.count_of_products)));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase(""+noOfProducts));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
Image imageFromWeb;
if(orderProductsList.size()>0) {
for (int i = 0; i < orderProductsList.size(); i++) {
String img = ConstantValues.ECOMMERCE_URL + orderProductsList.get(i).getImage();
try {
imageFromWeb = Image.getInstance(new URL(img));
imageFromWeb.setAbsolutePosition(400f, 150f);
imageFromWeb.scalePercent(100f);
imageFromWeb.scaleAbsoluteHeight(50);
imageFromWeb.scaleAbsoluteWidth((imageFromWeb.getWidth() * 50) / imageFromWeb.getHeight());
c1 = new PdfPCell(new Phrase(getResources().getString(R.string.total_price)));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(imageFromWeb);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
document.add(table);
}
//////这是运行应用程序后显示的错误:
2019-08-29 10:09:30.999 22114-22114/com.vectorcoder.androideco E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vectorcoder.androideco, PID: 22114
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
at java.net.InetAddress.getAllByName(InetAddress.java:1154)
at com.android.okhttp.Dns$1.lookup(Dns.java:39)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
at java.net.URL.openStream(URL.java:1072)
at com.itextpdf.text.Image.getInstance(Image.java:260)
at com.itextpdf.text.Image.getInstance(Image.java:241)
at com.vectorcoder.androideco.fragments.Invoice_Details.addTitlePage(Invoice_Details.java:395)
at com.vectorcoder.androideco.fragments.Invoice_Details.GenerateInvoicePdf(Invoice_Details.java:163)
at com.vectorcoder.androideco.fragments.Invoice_Details.onCreateView(Invoice_Details.java:126)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:7124)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:898)