在我们的Android应用程序中,我们有相册功能。哪些用户能够从他们的照片创建他们的专辑,之后我们希望用户能够从该专辑创建一个pdf文件。(我们在IOS中做同样的事情)
为了实现这个目标,我实施了以下解决方案。但是当我执行createPrintableFile方法时,它创建并清空pdf文件,我找不到原因? 有没有人对它有任何想法
package com.kidokit.kidokit.helper;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.pdf.PdfDocument;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.print.PrintAttributes;
import android.print.pdf.PrintedPdfDocument;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.kidokit.kidokit.R;
import com.kidokit.kidokit.network.NetworkModels;
import com.kidokit.kidokit.ui.StorybookView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MAPdfCreator
{
private FileOutputStream pdfFile = null;
private String filePath = null;
private Activity act;
private LayoutInflater inflater;
public MAPdfCreator(Activity act) throws FileNotFoundException
{
filePath = act.getFilesDir()+"/KidoKitAlbum.pdf";
pdfFile = new FileOutputStream(filePath);
this.act = act;
inflater = LayoutInflater.from(this.act);
}
public void createPrintableFile(List<NetworkModels.GetPhotosInAlbumRes.Photo> photos) throws IOException {
ArrayList<View> result = new ArrayList<View>();
int index = 0;
for (NetworkModels.GetPhotosInAlbumRes.Photo photo : photos) {
View myImageLayout = inflater.inflate(R.layout.view_storybook, null, false);
final StorybookView sbv = (StorybookView) myImageLayout.findViewById(R.id.sbv);
sbv.measure(480,853);
sbv.layout(0,0,480,853);
if (index == 0) {
/*
sbv.setTemplateLayout(R.layout.view_sb_cover);
sbv.setImage(photo.photoFile);
sbv.setLabel(photo.photoTitle);
sbv.setDate(photo.photoDate);
sbv.closeEditMode();
*/
} else {
sbv.setTemplateLayout(photo.getLayoutResource());
if (photo.photoFile != null && !photo.photoFile.equals("")) {
sbv.setImage(photo.photoFile);
}
if (photo.photoTitle != null) {
sbv.setLabel(photo.photoTitle);
}
if (photo.photoDate != null) {
sbv.setDate(photo.photoDate);
}
sbv.closeEditMode();
}
result.add(sbv);
index++;
}
convertViewsToPdf(result);
}
public void convertViewsToPdf(ArrayList<View> views) throws IOException {
if (Build.VERSION.SDK_INT >= 19) {
PrintAttributes printAttrs = new PrintAttributes.Builder().
setColorMode(PrintAttributes.COLOR_MODE_COLOR).
setMediaSize(PrintAttributes.MediaSize.NA_LETTER).
setResolution(new PrintAttributes.Resolution("KDK_LBL", "PRINT_SERVICE", 480, 853)).
setMinMargins(PrintAttributes.Margins.NO_MARGINS).
build();
PdfDocument document = new PdfDocument();
int index = 0;
for (View view : views) {
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(480,853,index).create();
PdfDocument.Page page = document.startPage(pageInfo);
view.draw(page.getCanvas());
document.finishPage(page);
index++;
}
document.writeTo(pdfFile);
document.close();
pdfFile.close();
File file = new File(filePath);
if(!file.exists()) {
System.out.println("FILE DOES NOT EXIST");
return;
}
this.openPdfFile();
}
}
public void openPdfFile()
{
Uri path = Uri.parse("content://"+act.getPackageName()+"/"+filePath);
Intent fileViewIntent = new Intent(Intent.ACTION_VIEW);
fileViewIntent.setDataAndType(path, "application/pdf");
String packageName = "com.adobe.reader";
fileViewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
fileViewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
act.startActivity(fileViewIntent);
} catch(ActivityNotFoundException e){
try {
act.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+packageName)));
} catch (android.content.ActivityNotFoundException anfe) {
act.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id="+packageName)));
}
} catch(Exception e){
Toast.makeText(act, "Can not open file: (" + filePath +")", Toast.LENGTH_SHORT).show();
}
}
}
这是调用abow createPrintableFile方法的代码
private void printAlbum(final int albumId)
{
NetworkManager.getPhotosInAlbum(albumId,new TokenCallback<NetworkModels.GetPhotosInAlbumRes>() {
@Override
public void onResponse(Call<NetworkModels.GetPhotosInAlbumRes> call, Response<NetworkModels.GetPhotosInAlbumRes> response) {
super.onResponse(call, response);
if (response.code() == 200) {
if (response.body().success) {
NetworkModels.GetPhotosInAlbumRes result = response.body();
try {
MAPdfCreator creator = new MAPdfCreator(currrentContext);
creator.createPrintableFile(result.photos);
} catch (java.io.IOException e) {
e.printStackTrace();
}
} else {
activity.progressDialog.dismiss();
activity.showSnackbar(activity.getText(R.string.unknown_error).toString());
}
} else {
activity.showSnackbar(activity.getText(R.string.server_error).toString());
}
}
@Override
public void onFailure(Call<NetworkModels.GetPhotosInAlbumRes> call, Throwable t) {
super.onFailure(call, t);
activity.showSnackbar(activity.getText(R.string.connection_error).toString());
activity.progressDialog.dismiss();
}
});
}