Skip to content

Commit

Permalink
adds the possibility to create a pdf from selected images to GalleryG…
Browse files Browse the repository at this point in the history
…ridActivity
  • Loading branch information
Your Name committed Jun 8, 2020
1 parent 7828ecc commit 9cff336
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 24 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ dependencies {
implementation 'com.github.ctodobom:FabToolbar:3c5f0e0ff1b6d5089e20b7da7157a604075ae943'
implementation 'org.piwik.sdk:piwik-sdk:0.0.4'
implementation 'com.github.MikeOrtiz:TouchImageView:1.4.0'
implementation 'com.itextpdf:kernel:7.1.11'
implementation 'com.itextpdf:layout:7.1.11'
implementation 'com.itextpdf:io:7.1.11'
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.FileProvider;
import android.support.v7.app.ActionBar;
Expand All @@ -30,12 +31,12 @@
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.todobom.opennotescanner.helpers.AboutFragment;
import com.todobom.opennotescanner.helpers.PdfHelper;
import com.todobom.opennotescanner.helpers.Utils;

import java.io.File;
import java.util.ArrayList;


public class GalleryGridActivity extends AppCompatActivity
implements ClickListener, DragSelectRecyclerViewAdapter.SelectionListener {

Expand Down Expand Up @@ -70,7 +71,7 @@ public void onLongClick(int index) {
}

private void setSelectionMode(boolean selectionMode) {
if (mShare !=null && mDelete != null ) {
if (mShare != null && mDelete != null) {
mShare.setVisible(selectionMode);
//mTag.setVisible(selectionMode);
mDelete.setVisible(selectionMode);
Expand All @@ -80,9 +81,9 @@ private void setSelectionMode(boolean selectionMode) {

@Override
public void onDragSelectionChanged(int i) {
Log.d(TAG, "DragSelectionChanged: "+i);
Log.d(TAG, "DragSelectionChanged: " + i);

setSelectionMode(i>0);
setSelectionMode(i > 0);
}


Expand All @@ -97,15 +98,15 @@ protected ThumbAdapter(GalleryGridActivity activity, ArrayList<String> files) {
super();
mCallback = activity;

for (String file : files){
for (String file : files) {
add(file);
}

setSelectionListener(activity);

}

void add(String path){
void add(String path) {
itemList.add(path);
}

Expand All @@ -121,13 +122,13 @@ public void onBindViewHolder(ThumbViewHolder holder, int position) {

String filename = itemList.get(position);

if ( !filename.equals(holder.filename)) {
if (!filename.equals(holder.filename)) {

// remove previous image
holder.image.setImageBitmap(null);

// Load image, decode it to Bitmap and return Bitmap to callback
mImageLoader.displayImage("file:///"+filename, holder.image, mTargetSize);
mImageLoader.displayImage("file:///" + filename, holder.image, mTargetSize);

// holder.image.setImageBitmap(decodeSampledBitmapFromUri(filename, 220, 220));

Expand All @@ -150,7 +151,7 @@ public ArrayList<String> getSelectedFiles() {

ArrayList<String> selection = new ArrayList<>();

for ( Integer i: getSelectedIndices() ) {
for (Integer i : getSelectedIndices()) {
selection.add(itemList.get(i));
}

Expand All @@ -159,7 +160,7 @@ public ArrayList<String> getSelectedFiles() {


public class ThumbViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener{
implements View.OnClickListener, View.OnLongClickListener {

public final ImageView image;
public String filename;
Expand Down Expand Up @@ -219,7 +220,7 @@ public void onCreate(Bundle savedInstanceState) {
mTargetSize = new ImageSize(220, 220); // result Bitmap will be fit to this size

ArrayList<String> ab = new ArrayList<>();
myThumbAdapter = new ThumbAdapter(this, ab );
myThumbAdapter = new ThumbAdapter(this, ab);
// new Utils(getApplicationContext()).getFilePaths(););

recyclerView = (DragSelectRecyclerView) findViewById(R.id.recyclerview);
Expand Down Expand Up @@ -248,6 +249,15 @@ public void onClick(DialogInterface dialog, int which) {
}
});

final FloatingActionButton pdfButton = (FloatingActionButton) findViewById(R.id.pdfButton);

pdfButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PdfHelper.mergeImagesToPdf(getApplicationContext(), myThumbAdapter.getSelectedFiles());
}
});

}

private void reloadAdapter() {
Expand All @@ -270,19 +280,18 @@ public void onResume() {
}

private void deleteImage() {
for ( String filePath: myThumbAdapter.getSelectedFiles() ) {
for (String filePath : myThumbAdapter.getSelectedFiles()) {
final File photoFile = new File(filePath);
if (photoFile.delete()) {
Utils.removeImageFromGallery(filePath,this);
Log.d(TAG,"Removed file: "+filePath);
Utils.removeImageFromGallery(filePath, this);
Log.d(TAG, "Removed file: " + filePath);
}
}

reloadAdapter();

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Expand All @@ -309,7 +318,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

switch(id) {
switch (id) {
case android.R.id.home:
finish();
break;
Expand Down Expand Up @@ -341,17 +350,17 @@ public void shareImages() {
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");

Uri uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName()+".fileprovider", new File(selectedFiles.get(0)));
Uri uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".fileprovider", new File(selectedFiles.get(0)));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
Log.d("GalleryGridActivity","uri "+uri);
Log.d("GalleryGridActivity", "uri " + uri);

startActivity(Intent.createChooser(shareIntent, getString(R.string.share_snackbar)));
} else {
ArrayList<Uri> filesUris = new ArrayList<>();
for (String i : myThumbAdapter.getSelectedFiles()) {
Uri uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName()+".fileprovider", new File(i));
Uri uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".fileprovider", new File(i));
filesUris.add(uri);
Log.d("GalleryGridActivity","uri "+uri);
Log.d("GalleryGridActivity", "uri " + uri);
}

final Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
Expand All @@ -364,5 +373,4 @@ public void shareImages() {
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.todobom.opennotescanner.helpers;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.todobom.opennotescanner.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;


public class PdfHelper {

public static void mergeImagesToPdf(Context applicationContext, ArrayList<String> files) {
//TODO move this to background thread
if (files.isEmpty()) {
Toast
.makeText(applicationContext, applicationContext.getString(R.string.no_files_selected), Toast.LENGTH_SHORT)
.show();
return;
}
String outputFile = "PDF-"
+ new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".pdf";

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
String pdfFilePath = new File(
android.os.Environment.getExternalStorageDirectory()
+ File.separator + sharedPreferences.getString("storage_folder", "OpenNoteScanner")
, outputFile)
.getAbsolutePath();

PdfWriter pdfWriter = null;
try {
pdfWriter = new PdfWriter(pdfFilePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(pdfWriter == null){
return;
}

PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument);

Collections.sort(files);

for (String file : files) {
ImageData imageData = null;
try {
imageData = ImageDataFactory.create(file);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if(imageData == null){
return;
}
Image image = new Image(imageData);
pdfDocument.addNewPage(new PageSize(image.getImageWidth(), image.getImageHeight()));
document.add(image);
}

document.close();

Toast
.makeText(applicationContext, pdfFilePath, Toast.LENGTH_SHORT)
.show();

}

}
11 changes: 11 additions & 0 deletions app/src/main/res/drawable-v21/ic_menu_pdf.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Taken from material.io
Apache license version 2.0 applies -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M20,2L8,2c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM11.5,9.5c0,0.83 -0.67,1.5 -1.5,1.5L9,11v2L7.5,13L7.5,7L10,7c0.83,0 1.5,0.67 1.5,1.5v1zM16.5,11.5c0,0.83 -0.67,1.5 -1.5,1.5h-2.5L12.5,7L15,7c0.83,0 1.5,0.67 1.5,1.5v3zM20.5,8.5L19,8.5v1h1.5L20.5,11L19,11v2h-1.5L17.5,7h3v1.5zM9,9.5h1v-1L9,8.5v1zM4,6L2,6v14c0,1.1 0.9,2 2,2h14v-2L4,20L4,6zM14,11.5h1v-3h-1v3z"/>
</vector>
21 changes: 18 additions & 3 deletions app/src/main/res/layout/activity_gallery.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="left|center_vertical"
android:background="#FFFFFF">

<com.afollestad.dragselectrecyclerview.DragSelectRecyclerView
Expand All @@ -13,5 +14,19 @@
android:layout_height="match_parent"
android:scrollbars="vertical" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/pdfButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="30dp"
android:layout_marginRight="30dp"
android:backgroundTint="#A060FF60"
android:src="@drawable/ic_menu_pdf"
android:tint="#ffffff"
app:borderWidth="0dp"
app:elevation="0sp"
app:fabSize="mini" />

</LinearLayout>
</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@
<string name="save_png_summary">Save final images to PNG format. Notice that metadata isn\'t supported yet on PNG, so some features will not be available.</string>
<string name="format_not_supported">Format not supported</string>
<string name="format_not_supported_message">Image format doesn\'t support tagging</string>
<string name="no_files_selected">No files selected.</string>
</resources>

0 comments on commit 9cff336

Please sign in to comment.