我正在尝试打开已保存到我的下载文件夹的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'是文件的名称。
logcat的:
12-18 03:24:30.978 24992-24992/com.example.paul.navigationdrawer I/View﹕ Touch down dispatch to android.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 android.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>
如果你想知道我想要做什么... mainactivity.java
package com.example.paul.navigationdrawer;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.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
android.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) {
android.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 android.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 android.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 android.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 android.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 android.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);
}
答案 0 :(得分:0)
我已经解决了这个问题,但感谢您的帮助!
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);
}