有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java My PDF未打开,locgcat中未显示错误

我正在尝试打开一个已保存到下载文件夹中的pdf。logcat中没有显示错误,当我单击按钮时,什么也没有发生

View view;
    public void tob(View view) {
        File file = new File("/sdcard/Download/mpsampletob.pdf");

        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) {
                Toast.makeText(MainActivity.this,
                        "No Application Available to View PDF",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

仅供参考,“tob”是按钮的名称,“mpsampletob”是文档的名称

日志:

12-18 03:24:30.978  24992-24992/com.example.paul.navigationdrawer I/View﹕ Touch down dispatch to 安卓.widget.Button{4284a098 VFED..C. ........ 169,731-486,827 #7f09005f app:id/tob}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=142.52289, y[0]=40.097534, Xw[0]=28.0, Yw[0]=28.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=198118232, downTime=198118232, deviceId=3, source=0x1002 }
12-18 03:24:31.142  24992-24992/com.example.paul.navigationdrawer I/View﹕ Touch up dispatch to 安卓.widget.Button{4284a098 VFED..C. ...p.... 169,731-486,827 #7f09005f app:id/tob}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=142.52289, y[0]=40.097534, Xw[0]=28.0, Yw[0]=28.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=198118398, downTime=198118232, deviceId=3, source=0x1002 }
12-18 03:24:31.156  24992-24992/com.example.paul.navigationdrawer D/OpenGLRenderer﹕ prepareDirty (0.00, 0.00, 720.00, 1280.00) opaque 1 <0x60085008>
12-18 03:24:31.157  24992-24992/com.example.paul.navigationdrawer D/OpenGLRenderer﹕ finish <0x60085008>
12-18 03:24:31.159  24992-24992/com.example.paul.navigationdrawer V/Provider/Settings﹕ from settings cache , name = sound_effects_enabled , value = 0
12-18 03:24:31.234  24992-24992/com.example.paul.navigationdrawer D/OpenGLRenderer﹕ prepareDirty (0.00, 0.00, 720.00, 1280.00) opaque 1 <0x60085008>
12-18 03:24:31.235  24992-24992/com.example.paul.navigationdrawer D/OpenGLRenderer﹕ finish <0x60085008>

如果你想知道我想做什么。。。主要活动。爪哇

package com.example.paul.navigationdrawer;

import 安卓.app.Activity;
import 安卓.content.ActivityNotFoundException;
import 安卓.content.Intent;
import 安卓.content.pm.PackageManager;
import 安卓.content.pm.ResolveInfo;
import 安卓.net.Uri;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.support.v7.app.ActionBar;
import 安卓.support.v4.app.Fragment;
import 安卓.support.v4.app.FragmentManager;
import 安卓.content.Context;
import 安卓.os.Build;
import 安卓.os.Bundle;
import 安卓.view.Gravity;
import 安卓.view.LayoutInflater;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.support.v4.widget.DrawerLayout;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.Button;
import 安卓.widget.TextView;
import 安卓.widget.Toast;

import org.apache.http.protocol.HTTP;

import java.io.File;
import java.util.List;


public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {
    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;
    private Button calloutsideuae;
    private Button callinsideuae;
    private Button emailamity;
    private Button tob;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        安卓.app.Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new HomeFragment();
                break;
            case 1:
                fragment = new HealthFragment();
                break;
            case 2:
                fragment = new PolicyFragment();
                break;
            case 3:
                fragment = new ProviderFragment();
                break;
            case 4:
                fragment = new ContactFragment();
                break;

            default:
                break;

        }
        if (fragment != null) {
        安卓.app.FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container,fragment).commit();}


    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 0:
                mTitle = getString(R.string.title_section1);
                break;
            case 1:
                mTitle = getString(R.string.title_section2);
                break;
            case 2:
                mTitle = getString(R.string.title_section3);
                break;
            case 3:
                mTitle = getString(R.string.title_section4);
                break;
            case 4:
                mTitle = getString(R.string.title_section5);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;

        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }


    public class HomeFragment extends 安卓.app.Fragment {
        private HomeFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_home, container, false);
            return view;
        }
    }

    public class HealthFragment extends 安卓.app.Fragment {
        public HealthFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_health, container, false);
            return view;
        }
    }

    public class PolicyFragment extends 安卓.app.Fragment {
        public PolicyFragment() {
        }

        private Button tob;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_policy, container, false);
            return view;
        }
    }


    public class ProviderFragment extends 安卓.app.Fragment {
        public ProviderFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_provider, container, false);
            return view;
        }
    }
    public class ContactFragment extends 安卓.app.Fragment {
        public ContactFragment() {
        }

        private Button calloutsideuae;
        private Button callinsideuae;
        private Button emailamity;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_contact, container, false);
            return rootView;
        }
    }

    View view;

    //OPEN / DOWNLOAD THE TOB********************************************************
    public void tob(View view) {
        File file = new File("/sdcard/Download/mpsampletob.pdf");

        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) {
                Toast.makeText(MainActivity.this,
                        "No Application Available to View PDF",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
    //CONTACT US FRAGMENT BUTTON ACTIONS******************************************************
    public void callinsideuae(View view) {
        //Build the intent
        Uri number = Uri.parse("tel:+971569576839");
        Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
        //Resolves?
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(callIntent, 0);
        boolean isIntentSafe = activities.size() > 0;
        //Start if safe to do so
        if (isIntentSafe) {
            startActivity(callIntent);
        }
    }
    public void calloutsideuae(View view){
        //Build the intent
        Uri number = Uri.parse("tel:+971569576839");
        Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
        //Resolves?
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(callIntent, 0);
        boolean isIntentSafe = activities.size() > 0;
        //Start if safe to do so
        if (isIntentSafe) {
            startActivity(callIntent);
        }
    }
    public void emailamity(View view){
        //Build the intent
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
// The intent does not have a URI, so declare the "text/plain" MIME type
        emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"paul.ali@amity.ae"}); // recipients
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text");
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));
// You can also attach multiple items by passing an ArrayList of Uris
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(emailIntent, 0);
        boolean isIntentSafe = activities.size() > 0;
        //Start if safe to do so
        if (isIntentSafe) {
            startActivity(emailIntent);
        }
    }

}

解决方案:

public void tob(View view) {
        Intent i=new Intent(Intent.ACTION_VIEW);
        File file = new File("/sdcard/Download/mpsampletob.pdf");


        i.setDataAndType(Uri.fromFile(file), "application/pdf");
        startActivity(i);

    }

共 (1) 个答案

  1. # 1 楼答案

    我已经解决了这个问题,但感谢您的帮助

    public void tob(View view) {
            Intent i=new Intent(Intent.ACTION_VIEW);
            File file = new File("/sdcard/Download/mpsampletob.pdf");
    
    
            i.setDataAndType(Uri.fromFile(file), "application/pdf");
            startActivity(i);
    
        }