0

我正在使用此功能将图像保存到 SD 卡,但我不断收到此错误

这是我的 saveImage 函数:

private void saveImage() {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... arg0) {
                @SuppressWarnings("deprecation")
                String FILENAME = getString(R.string.app_name)
                        + new Date().getDate();

                try {
                    @SuppressWarnings("unused")
                    FileOutputStream fos = null;
                    fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                parent.setDrawingCacheEnabled(true);
                Bitmap bitmap = parent.getDrawingCache();

                Canvas canvas = new Canvas(bitmap);
                parent.draw(canvas);
                // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

                File dir = new File(Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/" + getString(R.string.app_name));
                dir.mkdirs();

                String path = Environment.getExternalStorageDirectory()
                        .toString();
                OutputStream fOut = null;
                String ime = getIntent().getStringExtra("ime");
                File file = new File(dir.getAbsolutePath(), ime + ".jpg");
                try {
                    fOut = new FileOutputStream(file);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                try {
                    fOut.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    fOut.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    MediaStore.Images.Media.insertImage(getContentResolver(),
                            file.getAbsolutePath(), file.getName(),
                            file.getName());
                } catch (FileNotFoundException e) {
                    Toast.makeText(Draw.this, getString(R.string.error),
                            Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                Toast.makeText(getApplicationContext(), R.string.imageSaved_mk,
                        Toast.LENGTH_SHORT).show();
                super.onPostExecute(result);
            }
        }.execute();

    }

并从 logCat 输出:

08-04 15:30:37.369: W/System.err(6942): java.io.FileNotFoundException: /mnt/sdcard/Боенка/null.jpg (Permission denied)
08-04 15:30:37.395: W/System.err(6942):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
08-04 15:30:37.420: W/System.err(6942):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
08-04 15:30:37.444: W/System.err(6942):     at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
08-04 15:30:37.444: W/System.err(6942):     at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
08-04 15:30:37.444: W/System.err(6942):     at com.boenka.mk.Draw$2.doInBackground(Draw.java:147)
08-04 15:30:37.455: W/System.err(6942):     at com.boenka.mk.Draw$2.doInBackground(Draw.java:1)
08-04 15:30:37.492: W/System.err(6942):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-04 15:30:37.492: W/System.err(6942):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-04 15:30:37.492: W/System.err(6942):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-04 15:30:37.510: W/System.err(6942):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-04 15:30:37.510: W/System.err(6942):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-04 15:30:37.510: W/System.err(6942):     at java.lang.Thread.run(Thread.java:1096)
08-04 15:30:37.529: W/dalvikvm(6942): threadid=16: thread exiting with uncaught exception (group=0x4001d800)
08-04 15:30:37.561: E/AndroidRuntime(6942): FATAL EXCEPTION: AsyncTask #1
08-04 15:30:37.561: E/AndroidRuntime(6942): java.lang.RuntimeException: An error occured while executing doInBackground()
08-04 15:30:37.561: E/AndroidRuntime(6942):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-04 15:30:37.561: E/AndroidRuntime(6942):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-04 15:30:37.561: E/AndroidRuntime(6942):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-04 15:30:37.561: E/AndroidRuntime(6942):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-04 15:30:37.561: E/AndroidRuntime(6942):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-04 15:30:37.561: E/AndroidRuntime(6942):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-04 15:30:37.561: E/AndroidRuntime(6942):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-04 15:30:37.561: E/AndroidRuntime(6942):     at java.lang.Thread.run(Thread.java:1096)
08-04 15:30:37.561: E/AndroidRuntime(6942): Caused by: java.lang.NullPointerException
08-04 15:30:37.561: E/AndroidRuntime(6942):     at com.boenka.mk.Draw$2.doInBackground(Draw.java:154)
08-04 15:30:37.561: E/AndroidRuntime(6942):     at com.boenka.mk.Draw$2.doInBackground(Draw.java:1)
08-04 15:30:37.561: E/AndroidRuntime(6942):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-04 15:30:37.561: E/AndroidRuntime(6942):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-04 15:30:37.561: E/AndroidRuntime(6942):     ... 4 more

谁能帮我解决这个问题

4

1 回答 1

0
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {

        @Override
        protected Void doInBackground(byte[]... data) {
            FileOutputStream outStream = null;

            // Write to SD Card
            try {
                File sdCard = Environment.getExternalStorageDirectory();
                File dir = new File(sdCard.getAbsolutePath() + "/Name");
                dir.mkdirs();

                String fileName = "Any Name"
                File outFile = new File(dir, fileName);

                outStream = new FileOutputStream(outFile);
                outStream.write(data[0]);
                outStream.flush();
                outStream.close();



                refreshGallery(outFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            return null;
        }

    }
于 2015-12-28T07:42:25.133 回答