我正在尝试此代码共享来自appAsset的pdf文件,但显示错误 [英] I am trying this code to share pdf fromAsset of app But it's showing error

查看:130
本文介绍了我正在尝试此代码共享来自appAsset的pdf文件,但显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码从appset共享pdf,但显示错误

I am trying this code to share pdf fromAsset of app But it's showing error

InputStream outputFile = getAssets().open("new-age-careers-careers-that-didnt-exist-20-yr-ago.pdf"); Uri uri = Uri.fromFile((File)InputStream);

InputStream outputFile = getAssets().open("new-age-careers-careers-that-didnt-exist-20-yr-ago.pdf") ; Uri uri = Uri.fromFile( (File) InputStream );

**这是我的代码**

**Here is my code **

        Intent share = new Intent();
        share.setAction(Intent.ACTION_SEND);
        share.setType("application/pdf");
        share.putExtra(Intent.EXTRA_STREAM, uri);
        share.setPackage("com.whatsapp");

        activity.startActivity(share);

推荐答案

public class MainActivity extends AppCompatActivity {

ImageView share;
String pdfsname = "PAX-POS Manual.pdf";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    share = findViewById( R.id.share );

    share.setOnClickListener( new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
        @Override
        public void onClick(View view) {
            String message = "Choose today Enjoy tomorrow - Make right career choices";

            File fileBrochure = new File( Environment.getExternalStorageDirectory().getPath() + "/" + pdfsname );
            if (!fileBrochure.exists()) {
                CopyAssetsbrochure();
            }

            /** PDF reader code */
            File file = new File( Environment.getExternalStorageDirectory().getPath() + "/" + pdfsname );

            Intent intent = new Intent( Intent.ACTION_SEND );
            intent.putExtra( Intent.EXTRA_STREAM, Uri.fromFile( file ) );
            intent.setType( "application/pdf" );
          //  intent.putExtra( Intent.EXTRA_TEXT, message );
            intent.addFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION );
            intent.setPackage( "com.whatsapp" );

           intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                startActivity( intent );
            } catch (ActivityNotFoundException e) {
                Toast.makeText( MainActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT ).show();
            }
        }

        //method to write the PDFs file to sd card
        private void CopyAssetsbrochure() {
            AssetManager assetManager = getAssets();
            String[] files = null;
            try {
                files = assetManager.list( "" );
            } catch (IOException e) {
                Log.e( "tag", e.getMessage() );
            }
            for (int i = 0; i < files.length; i++) {
                String fStr = files[i];
                if (fStr.equalsIgnoreCase( pdfsname)) {
                    InputStream in = null;
                    OutputStream out = null;
                    try {
                        in = assetManager.open( files[i] );
                        out = new FileOutputStream( Environment.getExternalStorageDirectory() + "/" + files[i] );
                        copyFile( in, out );
                        in.close();
                        out.flush();
                        out.close();

                        break;
                    } catch (Exception e) {
                        Log.e( "tag", e.getMessage() );
                    }
                }
            }
        }
    } );
}

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);
    }
}

使用此代码即可正常工作

use this code it's working properly

这篇关于我正在尝试此代码共享来自appAsset的pdf文件,但显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆