2017-08-17 6 views
1

ファイルオーガナイザーアプリを作成しようとしていますが、共有オーディオ、ビデオ、または画像を受け取るアクティビティを作成したときにうまく動作しますが、そのファイルをUriから外部データストレージ内の特定のフォルダにコピーします。私は多くのコードを試して、関連するすべての質問を読みましたが、誰も私の問題を解決しません。Uriから外部データストレージの特定のフォルダにファイルをコピーする方法

これは私が試したものです:

<activity 
     android:name="nahamsoft.com.Home" 
     android:label="@string/title_activity_home" 
     android:theme="@style/AppTheme.NoActionBar"> 

     <intent-filter> 
      <action android:name="android.intent.action.SEND"/> 
      <action android:name="android.intent.action.SEND_MULTIPLE"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
      <data android:mimeType="application/vnd.google.panorama360+jpg"/> 
      <data android:mimeType="image/*"/> 
      <data android:mimeType="video/*"/> 
      <data android:mimeType="audio/*"/> 
     </intent-filter> 


     <intent-filter> 
      <action android:name="android.intent.action.SEND"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
      <data android:mimeType="text/plain"/> 
     </intent-filter> 
    </activity> 

Javaのタラ:

の権限がマニフェストで活動宣言

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

ですファイルを受信するためのeは次のとおりです。

if(Intent.ACTION_SEND.equals(action)&& type!=null){ 
     fab.show(); 
     Toast.makeText(this,"send action",Toast.LENGTH_LONG).show(); 
     if("text/plain".equals(type)){ 
      handleSentText(intent); 
     }else if(type.startsWith("image/")){ 
      try { 
       handleSentImage(intent); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     }else if(type.startsWith("video/")){ 
      handleSentVideo(intent); 
     }else if(type.startsWith("audio/")){ 
      handleSentAudio(intent); 
     } 
    } 
    else{ 
     Toast.makeText(this,"No data were come...",Toast.LENGTH_LONG).show(); 
    } 

ファイルをコピーする方法は次のとおりです。

private void handleSentAudio(Intent intent) { 

    Uri audio=(Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM); 
    Toast.makeText(this,"audio were handled",Toast.LENGTH_LONG).show(); 

} 

private void handleSentVideo(Intent intent) { 

     Toast.makeText(this,"Video were handled",Toast.LENGTH_LONG).show(); 
} 

private void handleSentImage(Intent intent) throws IOException { 
    Uri imageUri=(Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM); 


    /*try { 
     MoveFile(imageUri.getPath(),"/sdcard/Alarms/"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    }*/ 
    if(imageUri!=null){ 
     Toast.makeText(this,"from:"+imageUri.getPath()+"\n to :"+ 
       Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/",Toast.LENGTH_LONG).show(); 

     File src=new File(imageUri.toString()); 
     File des=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/"); 
     copyFile(src,des); 

    }else{ 
     Toast.makeText(this,"Image was not handled",Toast.LENGTH_LONG).show(); 
    } 
} 

private void handleSentText(Intent intent) { 
    String sharedText=intent.getStringExtra(Intent.EXTRA_TEXT); 

    Toast.makeText(this,"Text is come...",Toast.LENGTH_LONG).show(); 

} 

コピーファイル方式

public static void copyFile(File src, File dst) throws IOException 
{ 
FileChannel inChannel = new FileInputStream(src).getChannel(); 
FileChannel outChannel = new FileOutputStream(dst).getChannel(); 
try 
{ 
    inChannel.transferTo(0, inChannel.size(), outChannel); 
} 
finally 
{ 
    if (inChannel != null) 
     inChannel.close(); 
    if (outChannel != null) 
     outChannel.close(); 
} 
} 

私が欲しいのは、です。myFolderという特定のフォルダに処理したファイルを移動します。

答えて

0

置き換えます

File src=new File(imageUri.toString()); 

で:次に

InputStream src=getContentResolver().openInputStream(imageUri); 

  • 変更copyFile(src,des)があなたの目的地にInputStreamをコピーする

  • は、最終的には、バックグラウンドスレッドに、このI/Oを移動するので、あなたは、コピー操作の期間中の高速応答のための

+0

感謝をあなたのUIを凍結していないが、それは受け取るために私の対処ファイル方式を確認してくださいすることができます差がある2つのパラメータ –

+0

@ NahamAl-Zuhairi:正しい。最初のパラメータを 'InputStream'に変更する必要があります。そして、両方のファイルで作業しているわけではないので、 'FileChannel'を使用せずに、ストリームからストリームへコピーするメソッドを再実装する必要があります。 [これを行うためのコードスニペットがあります](https://github.com/commonsguy/cw-omnibus/blob/v8.7/ContentProvider/V4FileProvider/app/src/main/java/com/commonsware/android/cp /v4file/FilesCPDemo.java#L58-L69)。 – CommonsWare

+0

私はあなたの方法を試みたが、うまくいかなかった。私はdesフォルダをチェックしましたが、処理された画像は –

関連する問題