在Android中的Amazon S3中无需下载即可显示PDF文件

时间:2015-08-11 03:16:05

标签: java android pdf amazon-s3

我目前面临的问题是我想开发一个可以显示PDF文件的Android应用程序。

以下是我的代码:



public class S3Activity extends Activity {

    AmazonDynamoDB ddb;
    AmazonS3 s3;
    String bucket = "ftsmtheses";
    String destbucket = "afdal";

   public void S3Activity(String file) throws IOException{

        AmazonS3Client s3client = new AmazonS3Client(new PropertiesCredentials(CredentialsAuth.class.getClassLoader().getResourceAsStream("AwsCredentials.properties")));

            // Copying object
            CopyObjectRequest copyObjRequest = new CopyObjectRequest(
                    bucket, file, destbucket, file);
            System.out.println("Copying object.");
            s3client.copyObject(copyObjRequest);
       System.out.println("Done copying object.");

       ResponseHeaderOverrides override = new ResponseHeaderOverrides();
       override.setContentType("application/pdf");


           GeneratePresignedUrlRequest generatePresignedUrlRequest =
                   new GeneratePresignedUrlRequest(destbucket, file);
           generatePresignedUrlRequest.setMethod(HttpMethod.GET);

           URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest);
           System.out.println(url);

       try{
           Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url.toURI().toString()));
           startActivity(intent);
       }catch (URISyntaxException u){

       }




Amazon S3是我用来存储PDF文件的,我已经有了URL。我用这个:



Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url.toURI().toString()));
           startActivity(intent);




但它会给我带来错误。我试图搜索解决方案但失败了。我能以任何方式克服这个问题吗?



08-11 10:50:33.789  22050-22068/com.example.user.test2 W/System.err﹕java.lang.NullPointerException
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at android.app.Activity.startActivityForResult(Activity.java:3532)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at android.app.Activity.startActivityForResult(Activity.java:3458)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at android.app.Activity.startActivity(Activity.java:3780)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at android.app.Activity.startActivity(Activity.java:3748)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at com.example.user.test2.S3Activity.S3Activity(S3Activity.java:57)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at com.example.user.test2.MainActivity$Task.doInBackground(MainActivity.java:103)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at com.example.user.test2.MainActivity$Task.doInBackground(MainActivity.java:78)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-11 10:50:33.799  22050-22068/com.example.user.test2 W/System.err﹕ at java.lang.Thread.run(Thread.java:841)




0 个答案:

没有答案