Java 在pdf阅读器中从原始文件夹打开pdf文件
我有一些问题。我试着做一件事,当用户按下按钮时,pdf文件会在pdf阅读器中打开。我用程序写所有的东西,但它不起作用。有什么问题?你能给我写一个正确的代码吗?我做每件事都很谨慎。我的代码:Java 在pdf阅读器中从原始文件夹打开pdf文件,java,android,pdf,android-fragments,Java,Android,Pdf,Android Fragments,我有一些问题。我试着做一件事,当用户按下按钮时,pdf文件会在pdf阅读器中打开。我用程序写所有的东西,但它不起作用。有什么问题?你能给我写一个正确的代码吗?我做每件事都很谨慎。我的代码: package lt.sviesioji.kdainiviesiojigimnazija; import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; imp
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。资源是仅在开发计算机上的文件<代码>新文件(“R.id.matematika”)在Android设备上没有意义
第二,虽然有一个android.resource:Uri
方案可以使用,但很少有应用程序支持它
第三,虽然您可以将原始资源复制到一个文件,然后打开该文件
你的主要选择是:
- 将资源复制到上的文件,然后
- 使用,它可以直接从PDF的原始资源中提供PDF(尽管我建议您将其放在
中)assets/