问题
I have made an app in which i am successfully downloading pdf files from internet by URL. And sotre them to the app's internal storage with making a folder app_Pdf. But now I want to open that file with third party app like adobe pdf viewer...ect. I have tried so many way, i have goggling much for this issue. I have also read about making first content provider and serve pdf file by this questing : How to open a PDF stored in Internal Memory but i get the error : invalid path....
Please any buddy help me to solve this issue. It will be good if give me some working code Thank you very much in advance.....
This is how i download and store file in internal memory :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downloadFile("http://www.testdemo.net/testdemo/test/test/test.pdf");
}
public boolean downloadFile(String path)
{
try
{
URL url = new URL(path);
URLConnection ucon = url.openConnection();
ucon.setReadTimeout(5000);
ucon.setConnectTimeout(10000);
InputStream is = ucon.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
File file = new File(getDir("Pdf", Context.MODE_WORLD_READABLE) + "/yourfile.pdf");
if (file.exists())
{
file.delete();
}
file.createNewFile();
FileOutputStream outStream = new FileOutputStream(file);
byte[] buff = new byte[5 * 1024];
int len;
while ((len = inStream.read(buff)) != -1)
{
outStream.write(buff, 0, len);
}
outStream.flush();
outStream.close();
inStream.close();
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
}
and this is how i tried to view pdf from internal storage :
File file = new File(getDir("Pdf", Context.MODE_PRIVATE) + "/yourfile.pdf");
Uri internal = Uri.fromFile(file);
viewPdf(internal);
private void viewPdf(Uri file) {
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(file, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("No Application Found");
builder.setMessage("Download one from Android Market?");
builder.setPositiveButton("Yes, Please",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent.setData(Uri.parse("market://details?id=com.adobe.reader"));
startActivity(marketIntent);
}
});
builder.setNegativeButton("No, Thanks", null);
builder.create().show();
Log.v("","Exception : "+e);
}
Please help me....
来源:https://stackoverflow.com/questions/14832906/how-to-view-pdf-file-from-internal-storage-in-android-app