簡體   English   中英

無法下載 SD 卡中的圖像打開失敗:EPERM(不允許操作)

[英]Unable to download image in SD Card open failed: EPERM (Operation not permitted)

package com.rovedashcam.android.view.common.activity;


import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.rovedashcam.android.R;
import com.rovedashcam.android.view.base.BaseActivity;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class NewUploadPhoto extends BaseActivity {
    File directory;
    String downloadLink = "https://raw.githubusercontent.com/charly1811/google-books-api-android-sample/master/app/src/main/res/mipmap-xhdpi/ic_launcher.png";
    String fileName;
    String folderName;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_upload);
        folderName = "RoveDashCam/Photos";

        createFolderForApp(folderName);

    }


    private void createFolderForApp(String FolderName) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) + "/" + FolderName);
        } else {
            directory = new File(Environment.getExternalStorageDirectory() + "/" + FolderName);
        }
        Log.i("TAG", "createFolderForApp: " + directory.getPath());
        if (!directory.exists()) {
            boolean success = directory.mkdirs();
            if (!success) {
                directory = null;
            } else {
                new FileDownloader().execute(downloadLink);
            }
        } else {
            new FileDownloader().execute(downloadLink);
        }
    }

    private class FileDownloader extends AsyncTask<String, Void, String> {
        ProgressDialog progressDialog;

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

        @Override
        protected String doInBackground(String... params) {
            String result = "";
            if (!TextUtils.isEmpty(downloadLink)) {
                String MY_URL = downloadLink;
                try {
                    result = downloadFile(MY_URL);
                } catch (Exception e) {
                    Log.i("TAG", "doInBackground: " + e.getLocalizedMessage());
                }
            } else {
            }
            return result;
        }

        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            hideProgressDialog();
            Toast.makeText(NewUploadPhoto.this, result, Toast.LENGTH_SHORT).show();
        }
    }

    private String downloadFile(String MY_URL) {
        int count;

        try {
            URL url = new URL(MY_URL);
            URLConnection connection = url.openConnection();
            connection.connect();
            long startTime = System.currentTimeMillis();

            int lengthOfFile = connection.getContentLength();
            InputStream input = new BufferedInputStream(url.openStream(), 8192);
            fileName = MY_URL.substring(MY_URL.lastIndexOf('/') + 1, MY_URL.length());
            File outputFile = new File(directory, fileName);
            OutputStream output = new FileOutputStream(outputFile);
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                int status = (int) ((total * 100) / lengthOfFile);
                output.write(data, 0, count);
                new Handler().post(() -> {
                  showProgressDialog();
                    if (status == 100) {
                        hideProgressDialog();
                    }
                });
            }
            output.flush();
            output.close();
            input.close();
            return "Downloaded at: " + directory.getPath() + fileName;
        } catch (IOException e) {
            Log.e("Abhan", "Error: " + e.getLocalizedMessage());
            runOnUiThread(() -> hideProgressDialog());
        }
        hideProgressDialog();
        return "Downloading Error";
    }
}

這是我的代碼,我正在嘗試在給定文件夾“RoveDashCam/Photos”中的 SD 卡中下載圖像,但是當我收到錯誤 java.io.FileNotFoundException: /storage/emulated/0/Movies/RoveDashCam/Photos/ic_launcher.png: 打開失敗:EPERM(不允許操作)

我已經授予寫入外部和圖像讀取視頻讀取權限的權限然后我也收到錯誤任何人都可以幫助我在這方面我做錯了這段代碼如何我可以將圖像下載到 android 中的 sdCard。這只會發生android 13 電話。 android 10 我已經檢查過我能夠下載。

您不能在 Android 11+ 設備上的公共電影目錄中創建圖像文件。

只有視頻文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM