44import android .content .Intent ;
55import android .graphics .Bitmap ;
66import android .net .Uri ;
7+ import android .os .AsyncTask ;
78import android .os .Bundle ;
89import androidx .fragment .app .Fragment ;
910import androidx .appcompat .app .AlertDialog ;
1617import android .view .View ;
1718import android .view .ViewGroup ;
1819import android .widget .ImageView ;
20+ import android .widget .ProgressBar ;
1921import android .widget .Toolbar ;
2022
23+ import java .io .File ;
2124import java .io .FileNotFoundException ;
25+ import java .io .IOException ;
26+ import java .io .InputStream ;
2227import java .security .KeyStoreException ;
2328import java .security .NoSuchAlgorithmException ;
2429import java .security .UnrecoverableEntryException ;
30+ import java .util .concurrent .Executor ;
31+ import java .util .concurrent .Executors ;
2532
2633public 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}
0 commit comments