如何在Android Studio的内部存储中保存下载的文件?

时间:2019-03-05 06:28:55

标签: javascript android

我的应用程序中有一个下载按钮。当我按下下载按钮时,它会从提供的URL中下载音频文件。但是问题是我要将文件保存在名为“ DownloadTestFolder”的内部存储文件夹中,错误。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Download"
        android:onClick="Download"
        android:id="@+id/download"/>

</LinearLayout>
public class DownloadTest extends Activity {

    DownloadManager downloadManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.download_test);
    }

    public void Download(View view){

        downloadManager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri=Uri.parse("My Url");
        DownloadManager.Request request = new DownloadManager.Request(uri);
        try{
            request.setDestinationInExternalPublicDir(getFilesDir()+"/DownloadTestFolder","/");
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            Long reference = downloadManager.enqueue(request);

        }catch (Exception e){
            /* Error
        }

    }
}

2 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

 ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(getFilesDir().getName(), Context.MODE_PRIVATE);
File file =  new File(directory,”fileName”);
String data = “TEST DATA”;
FileOutputStream fos = new FileOutputStream(“fileName”, true); // save
fos.write(data.getBytes());
fos.close();

这会将文件写入设备的内部存储(/data/user/0/com.yourapp /)

答案 1 :(得分:0)

DownloadManager只能下载到external storage,您需要实现自己的下载管理器

  

1)添加读写权限

     

2)使用此方法保存文件

// DownloadFile AsyncTask
    private class DownloadFile extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Bitmap doInBackground(String... URL) {

            String imageURL = URL[0];

            Bitmap bitmap = null;
            try {
                // Download Image from URL
                InputStream input = new java.net.URL(URL).openStream();
                // Decode Bitmap
                bitmap = BitmapFactory.decodeStream(input);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {

                if (result != null) {
                    File destination = new File(context.getFilesDir(),
                            "/DownloadTestFolder/"+file_name + ".jpg");
                    try {
                        destination.createNewFile();
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        result.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
                        byte[] bitmapdata = bos.toByteArray();

                        FileOutputStream fos = new FileOutputStream(destination);
                        fos.write(bitmapdata);
                        fos.flush();
                        fos.close();
                        selectedFile = destination;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }



        }
    }
  

用法:-

new DownloadFile().execute("url_here);

注意:-我已经为图片类型文件添加了代码