Ne peut pas ouvrir le fichier PDF dans app externe
Je voudrais ouvrir un fichier PDF lorsque l'utilisateur clique sur un bouton.
Actuellement, je suis en utilisant ce code pour obtenir ceci:
Uri path = Uri.fromFile(new File("file:///android_asset/regola11_1.pdf"));
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);
mais ça ne fonctionne pas.
Lorsque je sélectionne à l'aide d'Adobe Acrobat, je reçois un message, affiché comme un Toast, qui dit
"This file could not be accessed Check the location or the network and try again."
Quand j'ai essayer avec le Lecteur PDF Viewer, je reçois
"Cannot display PDF ( regola11_1.pdf cannot be opened)"
Le fichier PDF est stocké dans
app > build > intermediates > assets
Où est le problème?
MODIFIER
Maintenant, je suis en utilisant le code suivant:
File file = new File("\"file:///android_asset/regola11_1.pdf");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
context.startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(context, "No application available to view PDF", Toast.LENGTH_LONG).show();
}
Mais lorsque j'essaie d'ouvrir le fichier PDF en cliquant sur le bouton, l'application se bloque.
C'est le journal que je reçois:
05-31 10:05:25.132 24474-24474/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.andrey.andreyvedis.iamaref, PID: 24474
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.startActivity(android.content.Intent)' on a null object reference
at com.andrey.andreyvedis.iamaref.FragmentR11.onClick(FragmentR11.java:147)
at android.view.View.performClick(View.java:4781)
at android.view.View$PerformClick.run(View.java:19873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
C'est ma classe:
public class FragmentR11 extends Fragment implements View.OnClickListener{
//TODO: Rename parameter arguments, choose names that match
//the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
Context context;
//TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentR11.
*/
//TODO: Rename and change types and number of parameters
public static FragmentR11 newInstance(String param1, String param2) {
FragmentR11 fragment = new FragmentR11();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public FragmentR11() {
//Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_r11, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().findViewById(R.id.bD1).setOnClickListener(this);
getActivity().findViewById(R.id.bD2).setOnClickListener(this);
getActivity().findViewById(R.id.bD3).setOnClickListener(this);
getActivity().findViewById(R.id.bD4).setOnClickListener(this);
getActivity().findViewById(R.id.bD5).setOnClickListener(this);
getActivity().findViewById(R.id.bD6).setOnClickListener(this);
getActivity().findViewById(R.id.bD7).setOnClickListener(this);
getActivity().findViewById(R.id.bD8).setOnClickListener(this);
getActivity().findViewById(R.id.bD9).setOnClickListener(this);
getActivity().findViewById(R.id.bD10).setOnClickListener(this);
getActivity().findViewById(R.id.bD11).setOnClickListener(this);
getActivity().findViewById(R.id.bD12).setOnClickListener(this);
getActivity().findViewById(R.id.bD13).setOnClickListener(this);
getActivity().findViewById(R.id.bD14).setOnClickListener(this);
getActivity().findViewById(R.id.bD15).setOnClickListener(this);
getActivity().findViewById(R.id.bD16).setOnClickListener(this);
getActivity().findViewById(R.id.bD17).setOnClickListener(this);
}
/**private void openPDF(final String pathToPDF) {
File file = new File(pathToPDF);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Devi installare un'app per aprire PDF, come Adobe Acrobat Reader ", Toast.LENGTH_SHORT).show();
}
}*/
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bD1: {
/**Uri path = Uri.fromFile(new File("regola11_1.pdf"));
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);
Toast.makeText(getActivity(), "Hai cliccato Regola 1 in Reg11 ", Toast.LENGTH_SHORT).show();*/
File file = new File("\"file:///android_asset/regola11_1.pdf");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
context.startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(context, "No application available to view PDF", Toast.LENGTH_LONG).show();
}
break;
}
case R.id.bD2:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 2 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD3:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 3 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD4:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 4 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD5:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 5 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD6:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 6 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD7:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 7 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD8:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 8 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD9:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 9 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD10:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 10 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD11:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 11 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD12:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 12 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD13:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 13 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD14:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 14 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD15:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 15 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD16:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 16 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
case R.id.bD17:
{
Toast.makeText(getActivity(), "Hai cliccato Regola 17 in Reg11 ", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
Quelqu'un peut m'aider?
EDIT 2
J'ai trouvé la solution, consultez ma réponse ci-dessous pour le code.
Merci à vous tous pour vos réponses.
OriginalL'auteur Andrey E. Vedishchev | 2015-05-30
Vous devez vous connecter pour publier un commentaire.
Ok les gars, le problème est résolu!
C'est le code que j'utilise pour ouvrir un fichier PDF, stockés sur mémoire externe:
obtenir
android.os.FileUriExposedException: file:///storage/emulated/0/Download/privacy_notice.pdf exposed beyond app through Intent.getData()
erreurHey @Sujay vous avez à donner à l'activité de la manipulation de votre intention de l'autorisation.
OriginalL'auteur Andrey E. Vedishchev
Applications tierces ne peuvent pas accéder à vos biens via
file:///android_asset
Url.Vous pouvez:
Essayez d'utiliser mon
StreamProvider
, d'avoir unContentProvider
dans votre application qui permet de desservir le fichier PDF directement à partir de l'actif, ouUtilisation
FileProvider
à partir de l'Android Support package, après avoir copié le fichier à partir d'actifs de stockage interne, comme il est démontré dans cet exemple d'application, ouCopier le fichier de stockage externe et utilisation d'une
Uri
créé à partir deUri.fromFile()
à point à qui copieSi vous êtes débutant, je vous recommande d'aller avec le troisième paragraphe de ma réponse.
Ok, mais, comment le faire?
troisième balle ne semble pas fonctionner sous android M
Deuxième balle + de l'application de ce sur l'intention: stackoverflow.com/a/33754937/1069884
OriginalL'auteur CommonsWare
Les données de réglage comme
"file:///" + filePath
fonctionné pour moi.OriginalL'auteur Sushant
Ajouter cette Autorisation et vérifier de nouveau :
il semble, il y a un problème avec l'accès au fichier.
Essayez d'ouvrir un autre fichier pdf et laissez-nous savoir quel est le Journal.
et je pense aussi qu'on ne peut pas utiliser ce :
file:///android_asset
et de prendre un coup d'oeil à ceci :
Choisissez le fichier avec
ExternalStorageDirectory
:À partir de : https://stackoverflow.com/a/17453242/4945820
Avec Des Actifs Dossier:
et voici le code, si vous utilisez ce avec
Assets
Dossier!puis ajouter de l'autorisation ci-dessus et vérifier de nouveau avec des Actifs de code.
Aussi, vérifiez ceci:
https://stackoverflow.com/q/28032029/4945820
Edition et mise à Jour.vous devez utiliser la deuxième partie de codes(à l'Actif)
De rien...c'est en donnant toujours les mêmes messages...
Il y a un problème avec cette Adresse :
"file:///android_asset/regola11_1.pdf"
&app > build > intermediates > assets
utiliser cette réponse pour créer un fichier Actifs : stackoverflow.com/a/26706866/4945820Je ne peux pas le croire. Il garde ne travaillent pas. Maintenant mes fichiers PDF sont dans "AppName>app>src>>des éléments d'actif. Lorsque j'essaie d'ouvrir dans mon application sur mon téléphone, je reçois le même message d'erreur. Je ne comprends pas pourquoi...
OriginalL'auteur developerdroid
J'ai eu le même problème, après des heures de traiter avec le problème que j'ai réalisé que j'avais manqué d'ajouter "/" entre le chemin du fichier et le nom de fichier.
Si le fichier n'a pas été trouvé et j'ai "Ce fichier n'a pas pu être consulté Vérifier l'emplacement ou le réseau et essayez de nouveau." message.
Espère que cela aide quelqu'un 😉
OriginalL'auteur Reza
Mon Problème est résolu après l'ajout de cette autorisation 🙂
OriginalL'auteur VINDHYA SHARMA
L'erreur est parce que chaque fois que le pdf téléchargement démarre à nouveau, mais depuis le téléchargement de cann pas être traitées dans cette période de temps limitée, il montre non valide erreur de format et de l'id de fichier est toujours dans le téléchargement de l'état.Utiliser le code..
OriginalL'auteur Abhishek Sirohi