Android:使用带有pdf文件的mediaStore

Android:使用带有pdf文件的mediaStore,android,download,mediastore,Android,Download,Mediastore,我尝试使用media store存储来自私有网络的pdf下载(在Environment.DIRECTORY\u DOWNLOADS中)并显示它。我希望用户能够选择应用程序来显示它。 在我的代码中,我很难定义文件路径“dataFile” 你能给点建议吗,谢谢 洛朗 dataFile应该是URI,可以从文件(或其路径)轻松创建 dataFile应该是URI,可以从文件(或其路径)轻松创建 我的解决方案经过多次测试: String[] projections = new String[]{

我尝试使用media store存储来自私有网络的pdf下载(在Environment.DIRECTORY\u DOWNLOADS中)并显示它。我希望用户能够选择应用程序来显示它。 在我的代码中,我很难定义文件路径“dataFile” 你能给点建议吗,谢谢 洛朗


dataFile
应该是
URI
,可以从
文件(或其路径)轻松创建


dataFile
应该是
URI
,可以从
文件(或其路径)轻松创建


我的解决方案经过多次测试:

   String[] projections = new String[]{
                    MediaStore.DownloadColumns._ID,
                };
   String selection =MediaStore.Files.FileColumns.DISPLAY_NAME+"=?";
   String[] selectionArgs = new String[]{newLocalpath};
   Cursor cursor = context.getContentResolver().query(
                        MediaStore.Downloads.EXTERNAL_CONTENT_URI,
                        projections,
                        selection,
                        selectionArgs,
                        null
                );
   Uri uri=null;
   int docIndex = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns._ID);
   if (cursor.moveToNext()==true) {
      uri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, cursor.getInt(docIndex));
   }
   if (uri!=null) {
      try {
         String fileType="application/pdf";
         String filetxt="Choose pdf Application";
         final Intent intent = new Intent(Intent.ACTION_VIEW)
                                .setDataAndType(uri, fileType);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         Intent intentChooser = Intent.createChooser(intent,filetxt);
         intentChooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intentChooser);
      } catch (Exception e) {
         Log.d("application", "error starting download file: " + e.toString());
      }

我的解决方案经过多次测试:

   String[] projections = new String[]{
                    MediaStore.DownloadColumns._ID,
                };
   String selection =MediaStore.Files.FileColumns.DISPLAY_NAME+"=?";
   String[] selectionArgs = new String[]{newLocalpath};
   Cursor cursor = context.getContentResolver().query(
                        MediaStore.Downloads.EXTERNAL_CONTENT_URI,
                        projections,
                        selection,
                        selectionArgs,
                        null
                );
   Uri uri=null;
   int docIndex = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns._ID);
   if (cursor.moveToNext()==true) {
      uri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, cursor.getInt(docIndex));
   }
   if (uri!=null) {
      try {
         String fileType="application/pdf";
         String filetxt="Choose pdf Application";
         final Intent intent = new Intent(Intent.ACTION_VIEW)
                                .setDataAndType(uri, fileType);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         Intent intentChooser = Intent.createChooser(intent,filetxt);
         intentChooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intentChooser);
      } catch (Exception e) {
         Log.d("application", "error starting download file: " + e.toString());
      }

我使用从mediaStore
File path=Environment.getExternalStoragePublicDirectory下载的目录(Environment.DIRECTORY\u下载);文件=新文件(路径、文件名)
它不是mediaStore,getExternalStoragePublicDirectory在API 29中被弃用,因此您的目录是
mediaStore.Downloads.EXTERNAL\u CONTENT\u URI
,使用
ContentResolver
创建您的文件URI,就像我使用从mediaStore下载的目录一样
文件路径=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_下载);File File=新文件(路径、文件名)
它不是mediaStore,getExternalStoragePublicDirectory在API 29中不推荐使用,因此您的目录是
mediaStore.Downloads.EXTERNAL\u CONTENT\u URI
,请使用
ContentResolver
创建您的文件URI,就像在第一次告诉您如何下载文件一样。您将使用一个URI或获得一个URI。使用该URI。使用该URI。使用mediaStorei、
尝试使用media store存储pdf下载
在使用ACTION_VIEW之前,您很好地解决了这个问题。告诉您已经解决了哪些问题。首先告诉您是如何下载文件的。您将使用或获得一个uri。使用该uri。使用media store uri。
尝试使用media store存储pdf下载
很好在你使用ACTION_VIEW之前,先告诉我你已经做了什么。
   String[] projections = new String[]{
                    MediaStore.DownloadColumns._ID,
                };
   String selection =MediaStore.Files.FileColumns.DISPLAY_NAME+"=?";
   String[] selectionArgs = new String[]{newLocalpath};
   Cursor cursor = context.getContentResolver().query(
                        MediaStore.Downloads.EXTERNAL_CONTENT_URI,
                        projections,
                        selection,
                        selectionArgs,
                        null
                );
   Uri uri=null;
   int docIndex = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns._ID);
   if (cursor.moveToNext()==true) {
      uri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, cursor.getInt(docIndex));
   }
   if (uri!=null) {
      try {
         String fileType="application/pdf";
         String filetxt="Choose pdf Application";
         final Intent intent = new Intent(Intent.ACTION_VIEW)
                                .setDataAndType(uri, fileType);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         Intent intentChooser = Intent.createChooser(intent,filetxt);
         intentChooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intentChooser);
      } catch (Exception e) {
         Log.d("application", "error starting download file: " + e.toString());
      }