2015-10-24 13 views
6

私は私たちのアプリ(Xamarin C#)私たちはサーバーからファイルをダウンロードします。我々は新たにダウンロードしたファイルにし、URIからURIを取得succefulダウンロードの終わりに、私たちは、ファイルのパスを取得:アンドロイド4.4.2でとAndroid 5 URIパスでAndroid 6ダウンロードファイルへのパスを取得

Android.Net.Uri uri = downloadManager.GetUriForDownloadedFile(entry.Value); 
path = u.EncodedPath; 

は、次のようになります。

uri="file:///storage/emulated/0/Download/2.zip" 
path = u.EncodedPath ="/storage/emulated/0/Download/2.zip" 

私たちは、そのファイルを処理するためにパスを使用しています。 問題は、Android 6(実際のネクサス電話上)に、我々は完全に異なるURIパスをすることを取得されています

uri="content://downloads/my_downloads/2802" 
path="/my_downloads/2802" 

これはにFileNotFound例外をスローすることにより、自分のコードを壊します。ダウンロードしたファイルが存在し、ダウンロードフォルダにあることに注意してください。 Android 6から取得したURIを使用して、適切なファイルパスを取得してファイルを処理できるようにするにはどうすればよいですか?

を使用すると、ダウンロードフォルダを使用してURIを構築することができる、 [email protected]

+0

@これは、これは私がやったことでSDK 22と23の両方で試してみましたSDK 22 – user1523271

+0

、同じ結果 – user1523271

答えて

0

ありがとう: Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toURI();

+1

で構築したが、それはAndroidの5でのみ動作し、6 Androidのない私は方法を見つけましたその周りにはいいURIがありませんが、ちょうどダウンロードしたファイルを開くことができます。だから私はファイルを開いて、どこにでもコピーします – user1523271

1

実際の要件はありませんでしたが、ファイルコンテンツを処理したいようです。そうであれば、ダウンロードされたファイルのファイル記述子を使用してファイル内容を読み取ることによって行うことができます。コードスニペットは

ParcelFileDescriptor parcelFd = null; 
    try { 
     parcelFd = mDownloadManager.openDownloadedFile(downloadId); 
     FileInputStream fileInputStream = new FileInputStream(parcelFd.getFileDescriptor()); 
    } catch (FileNotFoundException e) { 
     Log.w(TAG, "Error in opening file: " + e.getMessage(), e); 
    } finally { 
     if(parcelFd != null) { 
      try { 
       parcelFd.close(); 
      } catch (IOException e) { 
      } 
     } 
    } 

となりますが、処理後にそのファイルを移動または削除することも検討しています。

0

それは仕事です。 2016年6月24日

@Override 
public void onReceive(Context context, Intent intent) { 

    String action = intent.getAction(); 
    if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 
     DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE); 
     long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); 
     DownloadManager.Query query = new DownloadManager.Query(); 
     query.setFilterById(downloadId); 
     Cursor c = downloadManager.query(query); 
     if(c != null) { 
      if (c.moveToFirst()) { 
       int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS); 
       if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) { 
        String downloadFileUrl = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); 
        startInstall(context, Uri.parse(downloadFileUrl)); 
       } 
      } 
      c.close(); 
     } 
    } 
} 




private boolean startInstall(Context context, Uri uri) { 
    if(!new File(uri.getPath()).exists()) { 
     System.out.println(" local file has been deleted! "); 
     return false; 
    } 
    Intent intent = new Intent(); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.setAction(Intent.ACTION_VIEW); 
    intent.setDataAndType(uri, "application/vnd.android.package-archive"); 
    context.startActivity(intent); 
    return true; 
} 
関連する問題