[英]how to copy file from Uri to a specific folder in external data storage
我正在嘗試創建文件管理器應用程序,但是在創建一個活動來接收共享的音頻,視頻或圖像時,它可以正常工作,但是我想將該文件從Uri復制到外部數據存儲中的特定文件夾中,這是一個問題。 我已經嘗試了很多代碼並閱讀了所有相關問題,但是沒有人解決我的問題。
這是我嘗試的:
權限是
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
清單中的活動聲明:
<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代碼為:
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的特定文件夾中 。
更換:
File src=new File(imageUri.toString());
有:
InputStream src=getContentResolver().openInputStream(imageUri);
然后:
修改copyFile(src,des)
以將InputStream
復制到目標
最終將此I / O移至后台線程,因此在復制操作期間不會凍結UI
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.