Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//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();
}

}
});
Posted
Updated 27-Jun-15 2:37am
v2
Comments
Richard MacCutchan 27-Jun-15 8:54am    
Where does the problem occur? Also please edit your question and put your code within <pre></pre> tags to make it more readable.
Member 11796538 29-Jun-15 6:06am    
i am able to download file but not able to view it,no erros after compilation
Member 11796538 29-Jun-15 6:52am    
this is my code

String s = mdataarrayforNotes.getM_FilePath();

final String fileName = s.substring(s.lastIndexOf('/') + 1);
File filesname=new File(fileName);
Log.d("",fileName.toString());


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", filesname.toString());

if (filesname.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(String.valueOf(mydir), fileName);
String sumit= String.valueOf(request.setDestinationInExternalPublicDir(String.valueOf(mydir), fileName));


Log.d("Erorr save path", sumit);
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);


PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);

try
{

if (list.size() > 0 && f.isFile()) {
Intent intent1 = new Intent();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900