1

I'm writing an android app and it's my first time using Java. I'm trying to export a bitmap as a PNG file and save it to the directory. However, the FileOutPutStream always triggers the ioexception. I've checked the path by using isDirectory and it returns true. Could someone please look at my code to see where I get wrong?

protected void onActivityResult(int requestCode, int resultCode, Intent resultIntent) {
        TextView result = findViewById(R.id.textView);
        String state = Environment.getExternalStorageState();
        if (!state.equals(Environment.MEDIA_MOUNTED)) {
            return;
        }
        // Check which request we're responding to
        if (requestCode == PICK_IMAGE) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                //get image Uri
                Uri imageUri = resultIntent.getData();
                //get image path through Uri
                Bitmap bitmap;
                //getting path of external DCIM directory
                File dstPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                //checking if directory exists
                if(dstPath.isDirectory()) {
                    result.setText("True");

                }
                String filename = "test.png";
                File file = new File(dstPath,filename);
                //result.setText(file.toString());

                try {
                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                    //result.setText(bitmap.toString());
                    FileOutputStream out = new FileOutputStream(file);
                    result.setText("flag");
                    if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)==false){
                        result.setText("0");
                    }
                    else {
                        result.setText("1");
                    }
                    out.flush();
                    out.close();

                } catch(IOException e){

                    e.printStackTrace();
                    result.setText("false");
                }

            }
        }
    }

Here is the stack trace

2020-02-26 12:55:23.534 10261-10261/com.example.webviewproject E/YOUR_APP_LOG_TAG: I got an error
    java.io.FileNotFoundException: /storage/emulated/0/DCIM/test.png (Permission denied)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:308)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:180)
        at com.example.webviewproject.MainActivity.onActivityResult(MainActivity.java:102)
        at android.app.Activity.dispatchActivityResult(Activity.java:7454)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4353)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Yiming Sun
  • 13
  • 3
  • Please add the stacktrace also – shihabudheenk Feb 26 '20 at 17:48
  • Hi, thanks for the reply, I've just added the stack trace – Yiming Sun Feb 26 '20 at 17:57
  • 1
    Does this answer your question? [java.io.FileNotFoundException: /storage/emulated/0/downloadedfilem.jpg (Permission denied)](https://stackoverflow.com/questions/47688354/java-io-filenotfoundexception-storage-emulated-0-downloadedfilem-jpg-permissi) – shihabudheenk Feb 26 '20 at 18:41
  • Note that even with those permissions, this code will not work by default on Android 10 or higher: https://stackoverflow.com/q/57116335/115145 – CommonsWare Feb 26 '20 at 18:58

1 Answers1

-1

I think you need to give run time permissions to access the file

check this answer

Praveen K Y
  • 90
  • 1
  • 2
  • 9