[英]how to copy file from Uri to a specific folder in external data storage
I am trying to create a file organizer app but when created an activity to receive shared audio,video, or image it works fine but the problem that I want to copy that file from Uri to a specific folder in external data storage. 我正在尝试创建文件管理器应用程序,但是在创建一个活动来接收共享的音频,视频或图像时,它可以正常工作,但是我想将该文件从Uri复制到外部数据存储中的特定文件夹中,这是一个问题。 I have tried a lot of code and read all the related questions, but no one solve my problem. 我已经尝试了很多代码并阅读了所有相关问题,但是没有人解决我的问题。
this is what I tried: 这是我尝试的:
the permissions are 权限是
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
the activity declaration at manifest: 清单中的活动声明:
<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>
the Java code to receive file is: 接收文件的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();
}
the methods to copy the file are: 复制文件的方法是:
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();
}
copy file method 复制文件方法
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();
}
}
what I want is to move the handled file to specific folder which called myFolder . 我想要的是将已处理的文件移动到名为myFolder的特定文件夹中 。
Replace: 更换:
File src=new File(imageUri.toString());
with: 有:
InputStream src=getContentResolver().openInputStream(imageUri);
Then: 然后:
Modify copyFile(src,des)
to copy an InputStream
to your destination 修改copyFile(src,des)
以将InputStream
复制到目标
Eventually move this I/O to a background thread, so you do not freeze your UI for the duration of the copy operation 最终将此I / O移至后台线程,因此在复制操作期间不会冻结UI
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.