자산 폴더에 pdf 파일 (bang.pdf)을 넣었습니다. 버튼을 클릭하면 assets 폴더에서 pdf 파일을 열고 표시해야합니다.안드로이드의 asset 폴더에서 pdf 파일 열기
package com.example.pdfviewer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button but1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1 = (Button)findViewById(R.id.but1);
but1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"hai" , Toast.LENGTH_SHORT).show();
CopyReadAssets();
}
});
}
private void CopyReadAssets()
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "bang.pdf");
try
{
in = assetManager.open("bang.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/bang.pdf"),
"application/pdf");
startActivity(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
}
하지만 난 버튼을 클릭하면, 다음과 같은 오류가 오류가 표시됩니다 : 코드입니다 - android.content.ActivityNotFoundException을 : 활동이 의도 {행동 = android.intent.action.VIEW을 처리하기 위해 발견 DAT = 파일 : ///data/data/com.example.pdfviewer/files/bang.pdf의 일반 = 응용 프로그램/PDF} mainifestfile에서 : - 은 내가, 내 장치에 어도비 리더 애플리케이션을 설치 한 언급 여전히 동일한 오류 메시지가 나타납니다.
사전에이
덕분에
여기를 살펴보십시오. http://stackoverflow.com/questions/17085574/read-a-pdf-file-from-assets-folder –