【问题标题】:how to copy file from Uri to a specific folder in external data storage如何将文件从 Uri 复制到外部数据存储中的特定文件夹
【发布时间】:2017-08-17 21:54:06
【问题描述】:

我正在尝试创建一个文件管理器应用程序,但是当创建一个接收共享音频、视频或图像的活动时,它可以正常工作,但是我想将该文件从 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 的特定文件夹

【问题讨论】:

    标签: java android


    【解决方案1】:

    替换:

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

    与:

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

    然后:

    • 修改copyFile(src,des),将InputStream复制到您的目的地

    • 最终将此 I/O 移动到后台线程,因此您不会在复制操作期间冻结 UI

    【讨论】:

    • 感谢您的快速回复,但请检查我的应对文件方法,因为它接收到两个不同的参数
    • @NahamAl-Zuhairi:正确。您需要将第一个参数更改为InputStream。而且,由于您没有在双方都使用文件,因此您需要重新实现从流复制到流的方法,而不是使用FileChannelHere is a code snippet to do that.
    • 我试过你的方法,但没有奏效。我检查了des文件夹,但在中找不到处理的图像
    • @NahamAl-Zuhairi:“我检查了 des 文件夹,但处理的图像未在”中找到——也许你没有 get it indexed by the MediaStore
    • 我可以得到处理的文件名和类型和路径
    猜你喜欢
    • 1970-01-01
    • 2011-12-26
    • 2023-03-05
    • 2019-03-20
    • 2015-04-05
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多