2017-06-30 5 views
0

この場合は、受信時に.txtというファイルにwhats'appで問題があります。
私は私の活動に意図を取得:
共有ファイルを受信しました

if (Intent.ACTION_SEND.equals(action) && Objects.equals (type, "text/plain")) { 
    Log.d ("Intent", "have shared"); 
    Bundle bundle = intent.getExtras(); 
    Uri uri = (Uri) bundle.get(Intent.EXTRA_STREAM); 
    Log.d ("uri ", 
    uri.getAuthority()+" "+ 
    uri.getPath()+" "+ 
    uri.getFragment()+" "+ 
    uri.getUserInfo()); 
    File file = new File (uri.getPath()); 
    Log.d("file" , file.toString()+" "+file.getPath()+" "+file.getName()+" "+ file.exists()+" "+ file.isFile()+" "+file.isDirectory()+" "+file.isAbsolute()+" "+file.isHidden()); 
    try { 
     StringBuilder text = new StringBuilder(); 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 
     //read line by line 
     while ((line = br.readLine()) != null) { 
      text.append(line); 
      text.append('\n'); 
     } 
     Log.d ("String", text.toString()); 
     } catch (java.io.IOException e) { 
       e.printStackTrace(); 
     } 
:それは、 Bundleを通じて extraを抽出し、新しい Fileを作成し、 Stringにそれを変換しようと、 ACTION_SENDある場合

//check intent 
Intent intent = getIntent(); 
String action = intent.getAction(); 
String type = intent.getType(); 

制御

デバッグログの結果は次のとおりです。

D/uri: com.whatsapp.provider.media /item/95 null null 
D/file: /item/95 /item/95 95 false false false true false 

私はBufferedReaderに、このエラーを取得する:

W/System.err: java.io.FileNotFoundException: /item/95: open failed: ENOENT (No such file or directory) 

そして私はなぜ理解していません。
誰かが私を助けることができますか?

答えて

2

ファイルへのパスが指定されていません。あなたにはUriが与えられています。 A Uriはファイルではなく、Uriは具体的にはスキームであり、fileスキームではありません。

File file = new File (uri.getPath());を削除します。 new FileReader(file)new InputStreamReader(getContentResolver().openInputStream(uri))に置き換えて、contentfileの両方のスキームを処理します。

関連する問題