1 package cz.cvut.fel.dce.qrscanner;
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.res.AssetManager;
6 import android.graphics.Bitmap;
7 import android.graphics.BitmapFactory;
8 import android.graphics.RectF;
9 import android.net.Uri;
10 import android.os.Environment;
11 import android.support.annotation.Nullable;
12 import android.support.v7.app.ActionBarActivity;
13 import android.os.Bundle;
14 import android.util.Log;
15 import android.view.Menu;
16 import android.view.MenuItem;
17 import android.widget.ImageView;
18 import android.widget.Toast;
20 import com.sun.pdfview.PDFFile;
21 import com.sun.pdfview.PDFPage;
23 import net.sf.andpdf.nio.ByteBuffer;
25 import org.apache.commons.io.IOUtils;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
33 public class PreviewActivity extends ActionBarActivity {
34 public static final String TAG = "PreviewActivity";
35 public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
36 public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.png";
39 protected void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 setContentView(R.layout.activity_preview);
42 Intent intent = getIntent();
43 String comp_id = intent.getStringExtra("COMPONENT_ID");
44 if (comp_id != null) {
45 Log.i(TAG, "Received component id: " + comp_id);
46 String path = "/storage/sdcard0/Pictures" + SKODA_DOCS_PATH_EXTENSION + comp_id + "/";
47 String picturePath = path + SKODA_COMP_PICTURE_NAME;
48 Log.i(TAG, "Path to files: " + picturePath);
49 Bitmap previewBitmap = new BitmapFactory().decodeFile(picturePath);
50 if (previewBitmap != null) {
51 ImageView previewImg = (ImageView) findViewById(R.id.imgComponent);
52 previewImg.setImageBitmap(previewBitmap);
53 Uri uri = Uri.parse(path + "Bild_Arbeitseinleitung.pdf");
54 Intent pdfIntent = new Intent(this,MuPDFActivity.class);
55 pdfIntent.setAction(Intent.ACTION_VIEW);
56 pdfIntent.setData(uri);
57 startActivity(intent);
60 Toast toast = Toast.makeText(getApplicationContext(), "File not found", Toast.LENGTH_LONG);
66 Log.i(TAG, "No component id received");
73 public boolean onCreateOptionsMenu(Menu menu) {
74 // Inflate the menu; this adds items to the action bar if it is present.
75 getMenuInflater().inflate(R.menu.menu_preview, menu);
80 public boolean onOptionsItemSelected(MenuItem item) {
81 // Handle action bar item clicks here. The action bar will
82 // automatically handle clicks on the Home/Up button, so long
83 // as you specify a parent activity in AndroidManifest.xml.
84 int id = item.getItemId();
86 //noinspection SimplifiableIfStatement
87 if (id == R.id.action_settings) {
91 return super.onOptionsItemSelected(item);
95 * Use this to load a pdf file from your assets and render it to a Bitmap.
100 * of the pdf file in the assets.
104 private static Bitmap renderToBitmap(Context context, String filePath) {
106 InputStream inStream = null;
108 Log.d(TAG, "Attempting to copy this file: " + filePath);
109 inStream = new FileInputStream(new File(filePath));
110 bi = renderToBitmap(context, inStream);
111 } catch (IOException e) {
115 if (inStream != null) {
118 } catch (IOException e) {
119 // do nothing because the stream has already been closed
126 * Use this to render a pdf file given as InputStream to a Bitmap.
131 * the inputStream of the pdf file.
135 private static Bitmap renderToBitmap(Context context, InputStream inStream) {
138 byte[] decode = IOUtils.toByteArray(inStream);
139 ByteBuffer buf = ByteBuffer.wrap(decode);
140 PDFFile pdfFile = new PDFFile(buf);
141 int numPages = pdfFile.getNumPages();
142 Log.i(TAG, "Num pages: " + Integer.toString(numPages));
143 PDFPage mPdfPage = new PDFFile(buf).getPage(0);
144 float width = mPdfPage.getWidth();
145 float height = mPdfPage.getHeight();
147 bi = mPdfPage.getImage((int) (width), (int) (height), clip, false,
149 } catch (IOException e) {
154 } catch (IOException e) {
155 // do nothing because the stream has already been closed