[英]Open pdf file from raw folder in PDF Reader
I have some problem. 我有问题 I try to make one thing that when user press on the button, the pdf file opens in PDF Reader. 我尝试做一件事,当用户按下按钮时,pdf文件在PDF Reader中打开。 I write everything in program, but it doesn't work. 我用程序编写了所有内容,但没有用。 What is the problem? 问题是什么? Can you write me a code correctly? 你能给我写正确的代码吗? I do everything in fragement. 我做的一切都是断断续续的。 My code: 我的代码:
package lt.sviesioji.kdainiviesiojigimnazija;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.io.File;
import java.io.InputStream;
/**
* A simple {@link Fragment} subclass.
*/
public class FormulynasFragment extends Fragment {
public FormulynasFragment() {
}
Button f;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int backButtonCount = getFragmentManager().getBackStackEntryCount();
if (backButtonCount > 0) {
Fragment newFragment = new PagrindinisFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
final View rootView = inflater.inflate(R.layout.fragment_formulynas, container,
false);
f = (Button) rootView.findViewById(R.id.button69);
f.setOnClickListener(new View.OnClickListener() {
InputStream is = getResources().openRawResource(R.raw.matematika);
@Override
public void onClick(View v) {
startpdf();
}
private void startpdf() {
// TODO Auto-generated method stub
File file = new File("R.id.matematika");
if (file.exists()) {
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 {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
}
}
}
});
return rootView;
}
} }
You have a few problems. 你有几个问题。
First, R.id.matematika
is not a file. 首先, R.id.matematika
不是文件。 That is the ID of a resource. 那就是资源的ID。 A resource is a file only on your development machine. 资源仅是开发计算机上的文件。 new File("R.id.matematika")
is meaningless on an Android device. new File("R.id.matematika")
在Android设备上毫无意义。
Second, while there is an android.resource:
Uri
scheme that you could use, few apps support it. 其次,虽然您可以使用android.resource:
Uri
方案,但很少有应用程序支持它。
Third, while you could copy the raw resource to a file, then open the file, support for that is fading away . 第三,虽然您可以将原始资源复制到文件中,然后打开文件,但对该文件的支持正在逐渐消失 。
Your primary choices are: 您的主要选择是:
Copy the resource to a file on internal storage , then use FileProvider
将资源复制到内部存储器上的文件中,然后使用FileProvider
Use my StreamProvider
, which can serve your PDF straight from its raw resource (though I would recommend you put it in assets/
instead) 使用我的StreamProvider
,它可以直接从原始资源中为您的PDF提供服务(尽管我建议您将其放在assets/
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.