Skip to content

Commit 2b55044

Browse files
committed
bugs fixed, improvement of loading
1 parent c43041e commit 2b55044

File tree

13 files changed

+424
-212
lines changed

13 files changed

+424
-212
lines changed

app/src/main/java/com/katcom/androidFileVault/FileManager.java

Lines changed: 163 additions & 179 deletions
Large diffs are not rendered by default.

app/src/main/java/com/katcom/androidFileVault/ImageViewerFragment.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.Intent;
55
import android.graphics.Bitmap;
66
import android.net.Uri;
7+
import android.os.AsyncTask;
78
import android.os.Bundle;
89
import androidx.fragment.app.Fragment;
910
import androidx.appcompat.app.AlertDialog;
@@ -16,12 +17,18 @@
1617
import android.view.View;
1718
import android.view.ViewGroup;
1819
import android.widget.ImageView;
20+
import android.widget.ProgressBar;
1921
import android.widget.Toolbar;
2022

23+
import java.io.File;
2124
import java.io.FileNotFoundException;
25+
import java.io.IOException;
26+
import java.io.InputStream;
2227
import java.security.KeyStoreException;
2328
import java.security.NoSuchAlgorithmException;
2429
import java.security.UnrecoverableEntryException;
30+
import java.util.concurrent.Executor;
31+
import java.util.concurrent.Executors;
2532

2633
public class ImageViewerFragment extends Fragment {
2734
private static final String ARG_FILE = "file";
@@ -30,7 +37,9 @@ public class ImageViewerFragment extends Fragment {
3037
private ImageView mImageView;
3138
private ProtectedFile mFile;
3239
private int WRITE_REQUEST_CODE = 0;
33-
40+
private Bitmap picture;
41+
private ProgressBar mProgressBar;
42+
private Executor executor;
3443
@Override
3544
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
3645
View view = inflater.inflate(R.layout.fragment_image_viewer, container, false);
@@ -42,9 +51,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
4251
String imgName = mFile.getFilename();
4352

4453
mImageView = view.findViewById(R.id.fragment_image_viwer_image_view); // Bind the controller to the image view in the layout
54+
mImageView.setVisibility(View.GONE); // hide the image until it is loaded
4555

46-
mImageView.setImageBitmap(FileManager.get(getContext()).getPreview(mFile,700,700));
56+
mProgressBar = view.findViewById(R.id.image_viewer_progressBar);
57+
mProgressBar.setVisibility(View.VISIBLE); // Show the progress bar while loading
4758

59+
//mImageView.setImageBitmap(FileManager.get(getContext()).getPreview(mFile,700,700));
60+
executor = Executors.newFixedThreadPool(2);
61+
new LoadingImageTask().executeOnExecutor(executor,mFile);
4862
getActivity().setTitle(imgName);
4963
return view;
5064
}
@@ -99,9 +113,31 @@ private void export(Uri uri) {
99113
FileManager vault = FileManager.get(getContext());
100114
try {
101115
vault.exportFile(mFile,getContext().getContentResolver().openOutputStream(uri));
102-
} catch (FileNotFoundException | NoSuchAlgorithmException | KeyStoreException | UnrecoverableEntryException e) {
116+
} catch (NoSuchAlgorithmException | KeyStoreException | UnrecoverableEntryException | IOException e) {
103117
Log.e(TAG,e.toString());
104118
}
105119

106120
}
121+
122+
private class LoadingImageTask extends AsyncTask<ProtectedFile,Void,Void> {
123+
File file;
124+
InputStream in;
125+
126+
@Override
127+
protected Void doInBackground(ProtectedFile... files) {
128+
picture = new PreviewManager(getContext()).getPreview(mFile,700,700);
129+
return null;
130+
}
131+
132+
@Override
133+
protected void onPostExecute(Void aVoid) {
134+
updateUI();
135+
}
136+
}
137+
138+
private void updateUI() {
139+
mProgressBar.setVisibility(View.GONE);
140+
mImageView.setVisibility(View.VISIBLE);
141+
mImageView.setImageBitmap(picture);
142+
}
107143
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.katcom.androidFileVault;
2+
3+
import android.content.Context;
4+
import android.graphics.Bitmap;
5+
import android.graphics.BitmapFactory;
6+
import android.util.Log;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.security.KeyStoreException;
11+
import java.security.NoSuchAlgorithmException;
12+
import java.security.UnrecoverableEntryException;
13+
14+
public class PreviewManager {
15+
private Context mContext;
16+
private final static String TAG = "Preview Manager";
17+
public PreviewManager(Context context) {
18+
mContext =context;
19+
}
20+
21+
/**
22+
* This method returns a scaled thumbnail of a picture in form of a Bitmap,
23+
* given the size of the container of the thumbnail picture.
24+
* We need to make it run on the back ground for fast loading speed
25+
* @param file
26+
* @param sizeX
27+
* @param sizeY
28+
* @return
29+
*/
30+
public Bitmap getPreview(ProtectedFile file,int sizeX,int sizeY){
31+
Bitmap bitmap;
32+
try {
33+
FileManager vault = FileManager.get(mContext);
34+
35+
InputStream in = vault.getDecryptedInputStream(file.getFilepath());
36+
//ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
37+
BitmapFactory.Options options = new BitmapFactory.Options();
38+
options.inJustDecodeBounds = true;
39+
BitmapFactory.decodeStream(in,null,options);
40+
41+
float srcWidth = options.outWidth;
42+
float srcHeight = options.outHeight;
43+
String message = "Width : " + srcWidth + " Height : " + srcHeight;
44+
Log.i(TAG,message);
45+
46+
int inSampleSize = Utils.calculateInSampleSize(options,sizeX,sizeY);
47+
48+
options = new BitmapFactory.Options();
49+
options.inSampleSize = inSampleSize;
50+
in = vault.getDecryptedInputStream(file);
51+
//in.close();
52+
return BitmapFactory.decodeStream(in,null,options);
53+
} catch (UnrecoverableEntryException | NoSuchAlgorithmException | KeyStoreException | IOException e) {
54+
Log.e(TAG,e.toString());
55+
}
56+
57+
bitmap = getDefaultPreview();
58+
return bitmap;
59+
}
60+
61+
private Bitmap getDefaultPreview() {
62+
return BitmapFactory.decodeResource(mContext.getResources(),R.drawable.default_file_preview);
63+
}
64+
65+
66+
}

0 commit comments

Comments
 (0)