简体   繁体   English

如何将文件从Uri复制到外部数据存储中的特定文件夹

[英]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.

相关问题 在Android中将文件从内部存储复制到外部存储 - Copy file from the internal to the external storage in Android Android从内部存储复制文件到外部 - Android copy file from internal storage to external 如何将URI作为jpg文件保存到内部存储的自定义文件夹中? - How to save a URI as a jpg file to a custom folder on the internal storage? 如何等到具有根访问权限的文件从系统目录复制到外部存储之前 - How to wait until file copy from system directory to external storage with root access 来自外部存储的 Uri 上的 MediaPlayer 为空 - MediaPlayer is null on a Uri from external storage 从外部存储读取硬编码的 Uri - Read harcoded Uri from external storage 将文件从内部存储复制到外部存储 - Copy Files from Internal Storage to external storage 有没有办法将JAR内的资源文件以编程方式复制到外部文件夹? - Is there a way to programmatically copy a resource file from within a JAR to an external folder? 如何从文件路径获取文件 Uri? (范围存储) - How to get File Uri from file path? (Scoped Storage) 如何将共享首选项文件从内部存储传输到外部存储? - How to transfer sharedpreferences file from internal storage to external storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM