View to display PDFPage - android

I'm implementing some kind of magazine viewer (to browse pages like in android gallery), which content is taken from pdf file; pages must be zoomable. At first, I've tried to work with separate pictures from that pdf. I borrowed ImageViewTouch and ImageViewTouchViewPager. It works well, but I couldn't keep more than 3-4 big pics in memory (OutOfMemory error) , so that implementation is not suitable for me. (I need 5 big pictures in memory plus memory for about 50 little pictures to show table of contents)
So I've decided to work with pdf file. I've tried to display pdf with PdfViewer.jar, but this implementation with PDFViewerActivity is missing pictures or text when you try to zoom in or zoom out.
Can you advice me how to display my pdf like in android gallery? Or, at least, tell me, please, if you know views, which can display PDFPage with zooming option?
Edited: I do not need to open side app to view pdf. I need to dosplay it in my app.

You can give this a try:
public static void openPdf(Context context, String filename) {
File targetFile = new File(filename);
Uri targetUri = Uri.fromFile(targetFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
try {
context.startActivity(intent);
} catch (ActivityNotFoundException act ) {
Toast.makeText(context, "Please install a pdf reader to view help",Toast.LENGTH_LONG).show();
}
}

Related

Android OutOfMemory

What is my Problem (KISS)?
After showing the 3rd Image in Detail my App crashes cause OutOfMemory
Background information about my App:
I made an offline Supermarket. All products are shown in a ListView.
All Images I use are stored as String (Uri from every Image) in a sqlite Database by SugarORM.
The ListView element got a ArrayAdapter where the Information for each Item in the ListView is set.
Like this:
ImageView imageView = (ImageView) rowView.findViewById(R.id.productimage);
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
imageView.setImageURI(productList.get(position).getPictureUri());
handler.removeCallbacks(this);
}
});
My Intention was, to load the Images in the Background to Click on it, even before the Image is loaded (But it did'nt work for me :/ The Image is Shown, but I cant click on any other View Element while Loading the Image).
The "getPictureUri" gets the URI from the Picture that the Admin has choosen for the product with the following Code:
Intent pickPhoto = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto,1);
AndroidManifest.xml
android:hardwareAccelerated="false"
android:largeHeap="true"
I extend only from "AppCompatActivity" (Needed for Toolbar). Meaning of this: I don't use "Activity" even once.
Could anyone give me a good advice, how to clear the cached Images (I think this might be the Problem) and besides, maybe one of you got the Time to help me with my "Image Async Loading while you are able to move freely in your app" Problem.
Using setImageUri() is rather intensive on memory, especially when trying to load large bitmaps from a local resource. Instead, use tried and true solutions like Picasso or Glide for any/all image loading tasks. The libraries will work offline, can definitely load images from URIs pointing to a local resource and would most likely solve all your memory issues with image loading.

Unwanted image being saved in gallery

I have used an answer from here
How should I give images rounded corners in Android?
However when created the picture with rounded edges it is being saved in my camera gallery as well as in a custom location that I have specified, any reason why this would happen or anyway to get rid of it automatically?
I don't want a picture being saved here.
Images are not saved in your device gallery - the gallery just displays all the images that are on your phone, UNLESS they are in storage associated with particular application. Use the following method do determine where you should save the images.
private File getAbosoluteFile(String ralativePath, Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
return new File(context.getExternalFilesDir(null), ralativePath);
} else {
return new File(context.getFilesDir(), ralativePath);
}
}
It will store them in external storage associated with your application and they will seize being displayed in the device's gallery. Try this out with new image, because images are cached for a long time in the gallery application.

Open URL or PDFreader in custom screen Android

I am working on application where I need to open inbuilt apps in a custom screen like in Popup window
but when I invoke the inbuilt app the screen size is full screen while I need customize screen size. The inbuilt app can be anything like browser or pdfreader.
Here is my code :-
String strurl = "/sdcard/download/28889.pdf";
File file = new File(strurl);
if (file.exists()) {
Uri path = Uri.fromFile(file);
// Log.e("path of the file",path.toString());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(),
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
This opens up new window in my app but I need Popup kind of window where I need to show this pdfreader to get display.
By design, Android does not allow one app to run inside another (among other things, for obvious security reasons). So if that is what you want to do, you are out of luck.
BUT you can use a WebView (I let you consult the doc on this class) to display a webpage inside your app.
There are also some pdf libraries out there. A word of caution though, I worked on a pdf reader on Android 1 year ago and implementing a pdf reader was just not the best solution. Handling the pdf decoding on server side and serving a view of this image (or conversion to html) was the best solution.
This way you don't use too much computing power to read the pdf (surprisingly you need some serious cpu to read a pdf).
Things may have changed since I made that development (I was really constrained by the time factor) though but I still don't think that unless you explicitely need to display a pdf document and not just a document (for example a legitimate reason would be that you need to check the signature of the pdf on the Android device), you should not use the pdf format on a phone.
For web URL You can use Webview
For PDF
You need to make your pdf reader or you can use any open source pdf reader to make your own customized version of the pdf reader which you can use in your app.
Some of the them are
1) http://www.mupdf.com/
2) http://sourceforge.net/projects/pdfedit/
3) Search on Google/SO for other Links
Here is the Example code for pdf Reader:
http://lokeshatandroid.blogspot.in/2012/07/android-pdf-reader.html

Android: How to open a PDF file at a specific page

I have seen code here which will open a PDF file in a PDF viewer. Is it possible to modify the code to make the PDF viewer open the PDF at a specific page?
No. You cannot control 3rd party application unless it gives this possibility.
Yes there is a way to open a specific page in a pdf file
Inside Main Activity
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), pdfActivity.class);
intent.putExtra("pageno", 5); // add this to pass the page number
startActivity(intent);
}
});
Inside Pdf Activity
pdfView.fromAsset("SAMPLE_FILE.pdf")
.defaultPage(getIntent().getIntExtra("pageno",0)) // this is used to scroll to the page required
.enableAnnotationRendering(true)
.scrollHandle(new DefaultScrollHandle(this))
.load();
If you don't want it to pass through intent
pdfView.fromAsset("SAMPLE_FILE.pdf")
.defaultPage(65) // Write the number of the page
.enableAnnotationRendering(true)
.scrollHandle(new DefaultScrollHandle(this))
.load();
Yes You can open a Specific page in pdf file.
first of all create a global varble like
// give a defualt value
int pageNumber=0;
// secondly create a button in search dilaoge to find exact page in activty.
btn.setOnClickListner(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = 1;
String input = String.valueOf(textSearch.getText());
if (input != null && !input.equals("")) {
position =
Integer.valueOf(String.valueOf(textSearch.getText()));
}
// assign globar var pageNumber to finding position
pageNumber = position-1;
textSearch.setText("");
alertDialog1.dismiss();
}
// in Third Step just passed the pageNumber in defualt function.
pdfViewr.from("Your uri String ").default(pageNumber).load();
for ezpdf reader:
add intent.putExtra("page", 110);
But ezpdf is quite old. So I recommend you to checkout my new opensource pdf viewer and annotator project:
https://github.com/KnIfER/PolymPic
Intent it = new Intent(Intent.ACTION_VIEW)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("page", yourPageIndex)
.setDataAndType(yourUri, "application/pdf");
startActivity(it);
It's based on PDFium, which is fast and can even run pretty well on the android 4.4.
Andrejs has recommended that I should modify an open source pdf viewer app. When I become more confident at Android programming I may attempt that. Meanwhile, I am following a different tack. I am storing images of the individual PDF pages as separate jpg files and using WebView / WebViewClient to display the individual pages. These can be zoomed and panned (as in a PDF Viewer) and it is easy to make my app move to the next or previous page when I touch the screen near the edge of the page.
Open a PDF file to a specific page
To target an HTML link to a specific page in a PDF file, add #page=[page number] to the end of the link's URL.
For example, this HTML tag opens page 4 of a PDF file named myfile.pdf:
<A HREF="http://www.example.com/myfile.pdf#page=4">

Setting up remote bitmap faster in UI(ImageView)

I am writing an app that is essentially a flipcard that shows word/hint on one side and picture on other side relevant to it.I am using viewflipper for the two views.Problem is that the picture loads from internet.App access the db,extracts url and then loads picture.That means the change in view takes as much time as it takes to download the picture.I want to flip card immediately and load picture so that user do not thinks that app is slow.Rather they should know that picture is being loaded,hence the delay.Pls suggest improvement in code.My code for loading picture in flipcard is:
public void setBMP(String s) //String passed is url extracted from column of db uing
{ //internal db
try{
//String url1 = "c.getString(3)";
String url1= s;
System.out.println(url1);
URL ulrn = new URL(url1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
{
im.setImageBitmap(bmp);
}
else
System.out.println("The Bitmap is NULL");
}catch(Exception e){}
}
}
For changing view, i have set up actionListener.As soon as user touches screen card flips and image loads.
Also is it possible to preload the images in background while user is viewing some other card.Or is it possible to cache the cards viewed?
I would go the asnyctask route, because that way you can load/disable spinners (or wahtever loading animations) as well. Check out this answer for a really simple example. If you want to add spinners you need to start them in the onPreExecute() of the asnyctask (just add it to the example) and disable them in onPostExecute after you image is downloaded.
Using AsyncTask to load Images in ListView
it seems to me that creating a Runnable that gets the bmp and saves it to a hash map file and then, when it's needed if downloaded, it opens the file, and if not it downloads it from the web.
try looking at fedorvlasov's Lazy adapter for reference.
you can use his Image loader

Categories

Resources