在Android Studio中从手机浏览pdf

时间:2016-05-23 10:06:12

标签: pdf android-studio

我需要创建一个应用程序,包括从手机浏览pdf然后阅读它(稍后将在浏览部分完成后执行)并且我的代码出现问题。我已经浏览了我在stackoverflow上找到的以下链接,但遗憾的是它无法正常工作, Browse and upload pdf or word file in Android 这是我的MainActivity.java类

import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

public class MainActivity extends AppCompatActivity {

Button btnAttach;

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

    btnAttach = (Button) findViewById(R.id.button1);    
    //button1 is the id of the only button I have in my activity_main.xml

    // view products click event
    btnAttach.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            button1_OnClick(view);
            // Launching All products Activity

        }
    });
}


public void button1_OnClick(View view) {
    getDocument();
}


private void getDocument() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("application/msword,application/pdf");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    }

}
protected void onActivityResult ( int req, int result, Intent data)
{
    // TODO Auto-generated method stub
    super.onActivityResult(req, result, data);
    if (result == RESULT_OK) {
        Uri fileuri = data.getData();
     String   docFilePath = getFileNameByUri(this, fileuri);
    }
}

// get file path

private String getFileNameByUri(Context context, Uri uri) {
    String filepath = "";//default fileName
    File file;
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = context.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA, MediaStore.Images.Media.ORIENTATION}, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();

        String mImagePath = cursor.getString(column_index);
        cursor.close();
        filepath = mImagePath;

    } else if (uri.getScheme().compareTo("file") == 0) {
        try {
            file = new File(new URI(uri.toString()));
            if (file.exists())
                filepath = file.getAbsolutePath();

        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        filepath = uri.getPath();
    }
    return filepath;
}
}

1 个答案:

答案 0 :(得分:0)

@CherryDT指出,IEnumerable<IFoo> instantiatedFoos = unityContainer.GetAllInstantiatedObjects<IFoo>();失踪了。添加并修复了问题。