Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

filesystems - Android file copy prob in Android 10

I want to copy a image file to the scoped storage location. The image file is selected by user through any gallery app. The app is targeted to 29 and I am facing problem when I am running the code in device of API 29. Here is my code:

var photoSelectLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                    val proj = arrayOf(MediaStore.Audio.Media.DATA)
                    val cursor =
                        requireContext().contentResolver.query(imgUri, proj, null, null, null)
                    cursor?.let {
                        val columnIndex = it.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)
                        it.moveToFirst()
                        var issueImgPath = cursor.getString(columnIndex)
                        val selectedPhotoFile = File(issueImgPath)

                        if (selectedPhotoFile.length() / 1024 < 500) {

                            val timestamp = System.currentTimeMillis()

                val mediaStorageDir = File(activity.getExternalFilesDir("Android/data/com.app.project/files/Pictures"),"Images")

                if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) {
                    Log.d("File", "failed to create directory")
                }

                photoFile = File(mediaStorageDir.path + File.separator + "issue_photo_original_${timestamp}.jpg")

                            File(issueImgPath).copyTo(photoFile!!, true) // this line is giving problem
                           
                        }
                }
            }

Here is logcat:

Caused by: java.io.FileNotFoundException: /storage/emulated/0/Download/download.jpeg: open failed: EACCES (Permission denied)
        at libcore.io.IoBridge.open(IoBridge.java:496)
        at java.io.FileInputStream.<init>(FileInputStream.java:159)
        at kotlin.io.FilesKt__UtilsKt.copyTo(Utils.kt:235)
        at kotlin.io.FilesKt__UtilsKt.copyTo$default(Utils.kt:217)
        at com.beltmann.installation.ui.issues.LogIssueFragment$photoSelectLauncher$1.onActivityResult(LogIssueFragment.kt:838)
        at com.beltmann.installation.ui.issues.LogIssueFragment$photoSelectLauncher$1.onActivityResult(LogIssueFragment.kt:57)
        at androidx.activity.result.ActivityResultRegistry$1.onStateChanged(ActivityResultRegistry.java:145)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:3013)
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:586)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1636)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3112)
        at androidx.fragment.app.FragmentManager.dispatchStart(FragmentManager.java:3063)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:3017)
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:586)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1636)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3112)
        at androidx.fragment.app.FragmentManager.dispatchStart(FragmentManager.java:3063)
        at androidx.fragment.app.FragmentController.dispatchStart(FragmentController.java:262)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:482)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:210)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1425)
        at android.app.Activity.performStart(Activity.java:7825)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3294)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)?
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)?
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)?
     Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)

Please help me to solve this problem.

question from:https://stackoverflow.com/questions/65938410/android-file-copy-prob-in-android-10

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The image file is selected by user through any gallery app.

Then you will get a nice uri in onActivityResult. Use that uri.

Dont try to find a file path. You can open an input stream for the uri directly with

InputStream is = getContentResolver().openInoutStream(uri). 

Then use that stream as if it was the file input stream you used before (well not really).

So write a copy finction that takes an uri as source.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...