//this is my code of for download and view pdf in my android app using third party app,as of now i am not able view pdf but able to download it,,please rectify this code and give me response
String s = mdataarrayforNotes.getM_FilePath();
final String fileName = s.substring(s.lastIndexOf('/') + 1);
Log.d("",fileName.toString());
if (null != fileName && fileName.length() > 0 )
{
int endIndex = fileName.lastIndexOf(".");
if (endIndex != -1)
{
String newstr = fileName.substring(0, endIndex);
Log.d("error", newstr);
fNameExit = new File("/sdcard/WaliaApp/"+newstr);
}
}
Boolean isSDPresent = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if (isSDPresent)
{
// yes SD-card is present
final File mydir = new File("/sdcard/WaliaApp/");
Log.d("Error create path", mydir.toString());
if (!mydir.exists())
{
mydir.mkdirs();
}
else
{
Log.d("error", "dir. already exists");
}
// File myfile = new File("/sdcard/"+fileName);
// myfile.setReadable(true);
// myfile.setWritable(true);
Log.d("error check", fNameExit.toString());
if (fNameExit.exists())
{
Log.d("error", "File Avaliable.");
}
else
{
Uri source = Uri.parse(s);
DownloadManager.Request request = new DownloadManager.Request(source);
request.setDescription("Now Downloading FIle");
request.setTitle(fileName);
request.setMimeType("application/pdf");
request.setDestinationInExternalPublicDir(mydir.toString(),fileName);
Log.d("Erorr save path", mydir.toString());
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Notes.this.DOWNLOAD_SERVICE);
manager.enqueue(request);
BroadcastReceiver onComplete=new BroadcastReceiver()
{
public void onReceive(Context ctxt, Intent intent)
{
File f = new File("/sdcard/WaliaApp/"+fileName);
//.getAbsolutePath()
// f.setReadable(true);
Log.d("Error open path", f.toString());
Uri path = Uri.fromFile(f);
Log.d("Error open path111", path.toString());
Log.d("hii", path.toString());
try
{
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
//finish();
}
catch (ActivityNotFoundException e)
{
Toast.makeText(getApplicationContext(), "Unable to open File", Toast.LENGTH_SHORT).show();
}
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}
else
{
// Sorry
Toast.makeText(getApplicationContext(), "Sorry no SDCard Found...! ", Toast.LENGTH_SHORT).show();
}
}
});