I will show how to create your own mobile presentation viewer with Aspose.Slides API for Android.
This is the first article in a series, where I will pass you through some trivial and non-trivial scenarious, like: edit and convert presentation from mobile; upload presentation as a web application or add it to Aspose Cloud Storage; share presentation in various formats; generate unique presenation key; and many others.
Please stay tuned and share which other cases you are interested to see.👇
Presentation viewer for Android:
So, let us start with the first step - basic presentation viewer!👩💻
I will pass you through the key points, while you can find the whole project on GitHub.
Open Android Studio.
Install latest Aspose.Slides API for Android.
-
Define Main Layout with btnOpenPresentation button, or just take it from link.
My lookes simply this way: -
Create Main Activity and add this code to onCreate method, which will launch Presentation Activity by btnOpenPresentation click:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { // ... // Start Presentation activity on Open button click final Button btnOpenPresentation = findViewById(R.id.btnOpenPresentation); btnOpenPresentation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { execPresentationActivity(); }}); } // Start Presentation activity private void execPresentationActivity() { Intent openPresentationIntent = new Intent(MainActivity.this, PresentationActivity.class); startActivity(openPresentationIntent); } }
-
Create Presentation Layout with slides’ carousele and a control to show the current slide, or just take it from link.
My looks simply this way, but there is a lot of space to show creativity here👩🎨.
Actually, this is a final application we should get: -
Create Presentation Activity.
Now, let us stop more detailed.-
Define execFilePicker() method and call it from onCreate() in Presentation Activity. This method will open the file system, that we could choose the presentation file this way:
All this is done just in 3 lines:
public class PresentationActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { execFilePicker(); } // Open File system to choose presentation file private void execFilePicker() { Intent mRequestFileIntent = new Intent(Intent.ACTION_GET_CONTENT); mRequestFileIntent.setType(); startActivityForResult(mRequestFileIntent, READ_REQUEST_CODE); } }
-
Note that we’ve passed READ_REQUEST_CODE to startActivityForResult method. We will use it to check activity result in onActivityResult method and know if we can load the presentation chosen:
public class PresentationActivity extends AppCompatActivity { @Override public void onActivityResult(int requestCode, int resultCode, Intent resultData) { if (requestCode == READ_REQUEST_CODE && resultCode == RESULT_OK && resultData != null) { Uri uri = resultData.getData(); // Load chosen presentation execLoadPresentation(uri); }} }
-
In previous method we’ve called execLoadPresentation method to load chosen presentation.
Then selectSlide method can be called to select the slide of the presentation, which should be shown on Presentation Layout.
public class PresentationActivity extends AppCompatActivity { private SlidesApp app; // Invoke LoadPresentationTask to load chosen presentation private void execLoadPresentation(Uri uri) throws IOException { InputStream input = getContentResolver().openInputStream(uri); new LoadPresentationTask(this).execute(input); } // Invoke SelectSlideTask to select a slide to be shown private void selectSlide(int pos) { new SelectSlideTask(this).execute(pos); } }
-
-
Now lets create LoadPresentationTask and SelectSlideTask classes used in two previous methods.
LoadPresentationTask will do the following:
- doInBackground - set the presenation, choosed by user, as the current one.
private static class LoadPresentationTask extends AsyncTask<InputStream, Void, Presentation> { // Set chosen presentation as current one @Override protected Presentation doInBackground(InputStream... inputs) { InputStream input = inputs[0]; Presentation p = input == null ? new Presentation() : new Presentation(input); // Call setPresentation by SlidesApp to set chosen presentation as current one presentationActivity.app.setPresentation(p, presentationActivity.currSlideViewSize, presentationActivity.slidesListViewSize); return p; } }
SelectSlideTask will do the following:
- doInBackground - select the slide of presentation to be shown on Presentation Layout.
private static class SelectSlideTask extends AsyncTask<Integer, Void, Bitmap> { // Select the slide, that should be shown @Override protected Bitmap doInBackground(Integer... ints) { int pos = ints[0]; return presentationActivity.app.selectSlide(pos); } }
These tasks refer to presentationActivity.app.setPresentation() and presentationActivity.app.selectSlide(), where presentationActivity.app - is the instance of SlidesApp class, which manages all logic about Presentation and Slide objects.
-
In the end, we came to the main part of the story.👩🏫
❗️THE MAGIC STARTS HERE!❗️SlidesApp class incapsulates all the logic to work with Aspose.Slides API for Android.
This class will allow us to locally save presentation, get all slides, set current slide, convert it into the thumbnail, calculate thumbnail’s size, etc.-
Import IPresentation, ISlide, ISlideCollection types:
import com.aspose.slides.IPresentation; import com.aspose.slides.ISlide; import com.aspose.slides.ISlideCollection;
-
Add SlidesApp class with the instances of current presentation and current slide:
public class SlidesApp extends Application { private IPresentation presentation; private Bitmap currSlide; //... }
-
Now lets implement selectSlide method, that will call getSlides to retrieve all presentation slides, then get the slide needed by its id. And, get the thumbnail of the slide with getThumbnail method.
public class SlidesApp extends Application { public Bitmap selectSlide(int idx) { //... this.currSlide = this.presentation.getSlides().get_Item(idx).getThumbnail(this.slideThumbnailSize); return this.currSlide; } }
-
The thumbnail is saved into currSlide property and returned back into SelectSlideTask, which will draw it on Presentation Layout:
public class PresentationActivity extends AppCompatActivity { private ImageView currSlide; //... } private static class SelectSlideTask extends AsyncTask<Integer, Void, Bitmap> { private final PresentationActivity presentationActivity; @Override protected void onPostExecute(Bitmap result) { // Set thumbnail to currSlide image view, that it could be drawn on Presentation Layout presentationActivity.currSlide.setImageBitmap(result); } }
-
I have shown you how to developer a basic presentation viewer, while there are a lot of features you can easily add to this application with Aspose.Slides API for Android to edit, convert or manage presentation or a certain slide. Check it out here.👈
Top comments (0)