Looking at the following website:
https://github.com/jblough/Android-Pdf-Viewer-Library
He's listed the following code:
Intent intent = new Intent(this, YourPdfViewerActivity.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, "PATH TO PDF GOES HERE");
startActivity(intent);
What will that path be and where I be placing this PDF file?
I have placed a test PDF file in assests folder, res/raw. Pushed a file from the ddms in data/data/project, and in libs folder as well. I tried the following but I am still getting file not found:
String path = Environment.getExternalStorageDirectory().getPath();
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "test.pdf";
String path = "android.resource://com.example.test3/raw/test.pdf";
String path = "file:///android_assets/test.pdf";
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.pdf";
final File cacheDir = context.getDir("cache", 0);
if(!cacheDir.exists()) cacheDir.mkdirs();
final File fileContent = new File(cacheDir, "test1.pdf");
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test1.pdf";
path = fileContent.getPath();
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
startActivity(intent);
Ran it first time, it created that folder, then I placed the file in that folder and ran it again and it was able to find the file. Placed the file from the ddms. Answered by Selvin.
The easiest, which worked for me:
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "pdfName.pdf");
String path = f.getPath();
Intent intent = new Intent(this, BulaActivity.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
startActivity(intent);
Related
I'm trying to share a file from my android app, but I'm getting an error like file not supported or unknown file.
CommonMethods commonMethods = new CommonMethods();
String internalFile = commonMethods.getDate();
String sharepath = Environment.getExternalStorageDirectory() + "/My Records/" + internalFile + "/";
Uri urii = Uri.parse(sharepath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM,urii);
startActivity(Intent.createChooser(share,"Share Sound File"));
I am creating an app where every time press the download button it's create a pdf file in my storage.My problem is newly created file overwrites the existing file with same file name.I need to download with new filename.What i am trying
dirpath =
android.os.Environment.getExternalStorageDirectory().toString();
// int increase=0;
file = new File(dirpath+"/NewPDF.pdf");
if(file.exists()){
increase++;
file = new File(dirpath + "/NewPDF" +increase+".pdf");}
This above lines of program create files but i need to open the last download file
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File( file,"/NewPDF.pdf"));
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
These above lines of code generate an error.
Error:File no longer exist,the file maybe deleted or changed or
removed by another program
1.create a new file and prevent overwiting whenever i click the button
2.open the last downloaded file
I am new to Android and little confused about that and kindly need some help from you guys.
You call Uri uri = Uri.fromFile(new File( file,"/NewPDF.pdf")); for open file, so why you added /NewPDF.pdf because its already in your file. just remove it.
Replace this line
Uri uri = Uri.fromFile(new File( file,"/NewPDF.pdf"));
to
Uri uri = Uri.fromFile(new File( file));
You are assigning the same name to every file you create. Try giving a unique name like this:
UUID uuid = UUID.randomUUID();
String randomUUIDString = uuid.toString();
the randomUUIDString will be your unique string for the pdf files. Also, store these strings in a SQLite db if you wish to use them.
The path you have entered in intent not matching with the created file path.
Try this.
dirpath = android.os.Environment.getExternalStorageDirectory().toString();
String lastFilePath = null;
// int increase=0;
file = new File(dirpath+"/NewPDF.pdf");
if(file.exists()){
increase++;
file = new File(dirpath + "/NewPDF" +increase+".pdf");}
lastFilePath = file.getAbsolutePath();
Then you can open the file like this.
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(lastFilePath));
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Hope it helps:)
Replace if with while and display Intent:
dirpath = android.os.Environment.getExternalStorageDirectory().toString();
int increase = 0;
file = new File(dirpath+"/NewPDF.pdf");
while(file.exists()) {
increase++;
file = new File(dirpath + "/NewPDF" +increase+".pdf");
}
file.createNewFile();
... store data in file here ...
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Use Time stamp instead of Counter, so that you can create a new file every time.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
Date timeStamp = new Date();
dirpath =
android.os.Environment.getExternalStorageDirectory().toString();
// int increase=0;
file = new File(dirpath+"/NewPDF.pdf");
if(file.exists()){
increase++;
file = new File(dirpath + "/NewPDF" +formatter.format(timeStamp )+".pdf");}
I would like to check if a certain file exists on an USB-stick, plugged in over OTG-Adapter.
Following code returns always false, no matter what happens. The file can be opened when deleting the if clause.
File usb_dir = Environment.getExternalStorageDirectory();
File myFile = new File(usb_dir+ "/update.apk");
if (myFile.exists()==true) {
final Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse("file:///mnt/usb_storage/update.apk"), "application/vnd.android.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(promptInstall);
} else if (myFile.exists()==false){
Toast.makeText(getApplicationContext(), "Not working!", Toast.LENGTH_LONG).show();
}
even this is not working:
String usb_dir = Environment.getExternalStorageDirectory().getAbsolutePath();
File myFile = new File(usb_dir+ "/update.apk");
Can anyone help me out there?
I'm trying to check if a certain file exists on a plugged usb-stick. This is the code:
File usb_dir = Environment.getExternalStorageDirectory();
File myFile = new File(usb_dir+ "/update.apk");
if (myFile.exists()==true) {
final Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse("file:///mnt/usb_storage/update.apk"), "application/vnd.android.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(promptInstall);
} else if (myFile.exists()==false){
Toast.makeText(getApplicationContext(), "Not working!", Toast.LENGTH_LONG).show();
}
everything works fine instead of myFile.exists. no matter what I do it returns false. Any idea how to fix that?
Try using:
String usb_dir = Environment.getExternalStorageDirectory().getAbsolutePath();
File myFile = new File(usb_dir+ "/update.apk");
This is the code i am using to create a folder in the default pictures folder:
File imagesFolder = new File(Environment.DIRECTORY_PICTURES, "/images");
if (!imagesFolder.exists()) {
Log.d("if imagesFolder exists - 1", "False");
imagesFolder.mkdirs();
} else {
Log.d("if imagesFolder exists - 1", "True");
}
if (!imagesFolder.exists()) {
Log.d("if imagesFolder exists - 2", "False");
imagesFolder.mkdirs();
} else {
Log.d("if imagesFolder exists - 2", "True");
}
in log i am getting:
False
False
for the 1st time the directory is not present, hence False but then immediately i am creating it using mkdirs(), hence i expect the 2nd log to be True but even that is False and my application crashed because of NullPointerException in the later part of the code
Please help
Thank You
You are using Environment.DIRECTORY_PICTURES the wrong way. It's just a String constant like "Pictures" but not a path. You need to get the path via Environment.getExternalStoragePublicDirectory(string)
File pictureFolder = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
);
File imagesFolder = new File(pictureFolder, "images");
// etc
To generate a folder at first you need to add permission at AndroidMinifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Now call following method to create a new folder with in which Directory you want to create your folder(this name should exist in Environment. list) and your folder name.
File outputDirectory = GetPhotoDirectory(Environment.DIRECTORY_PICTURES, "YourFolder");
Generate your folder by this method
public static File GetDirectory(String inWhichFolder, String yourFolderName ) {
File outputDirectory = null;
String externalStorageState = Environment.getExternalStorageState();
if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
File pictureDirectory = Environment.getExternalStoragePublicDirectory(inWhichFolder);
outputDirectory = new File(pictureDirectory, yourFolderName);
if (!outputDirectory.exists()) {
if (!outputDirectory.mkdirs()) {
Log.e(LogHelper.LogTag, "Failed to create directory: " + outputDirectory.getAbsolutePath());
outputDirectory = null;
}
}
}
return outputDirectory;
}
If you want to create a file under your newly created folder then you can use below code
public static Uri GenerateTimeStampPhotoFileUri(File outputDirectory, String fileName){
Uri photoFileUri = null;
if(outputDirectory!=null) {
File photoFile = new File(outputDirectory, fileName);
photoFileUri = Uri.fromFile(photoFile);
}
return photoFileUri;
}
Call to create a file after creating the folder with File Directory. It will return your file Uri
Uri fileUri = GenerateTimeStampPhotoFileUri(outputDirectory, fileName);