[英]Open pdf file from raw folder in PDF Reader
我有问题 我尝试做一件事,当用户按下按钮时,pdf文件在PDF Reader中打开。 我用程序编写了所有内容,但没有用。 问题是什么? 你能给我写正确的代码吗? 我做的一切都是断断续续的。 我的代码:
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;
}
}
你有几个问题。
首先, R.id.matematika
不是文件。 那就是资源的ID。 资源仅是开发计算机上的文件。 new File("R.id.matematika")
在Android设备上毫无意义。
其次,虽然您可以使用android.resource:
Uri
方案,但很少有应用程序支持它。
第三,虽然您可以将原始资源复制到文件中,然后打开文件,但对该文件的支持正在逐渐消失 。
您的主要选择是:
将资源复制到内部存储器上的文件中,然后使用FileProvider
使用我的StreamProvider
,它可以直接从原始资源中为您的PDF提供服务(尽管我建议您将其放在assets/
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.