我正在尝试创建一个文件管理器应用程序,但是当创建一个活动来接收共享音频,视频或图像时,它工作正常,但我想将该文件从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的特定文件夹。
答案 0 :(得分:1)
替换:
File src=new File(imageUri.toString());
使用:
InputStream src=getContentResolver().openInputStream(imageUri);
然后:
修改copyFile(src,des)
以将InputStream
复制到目的地
最终将此I / O移至后台线程,因此您不会在复制操作期间冻结UI