将图像异步保存到SD卡

时间:2013-08-11 09:46:40

标签: android android-asynctask android-sdcard android-file

我正在使用此功能将图像保存到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

任何人都可以帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

<div style="padding: 0px;height:80px;width:30%;align:center; background:green;display:table" >
      <ion-scroll class="center" scrollbar-x=false direction="x" 
            style="text-align:center;background-color:pink;display:table-cell; vertical-align:middle;height: 80px;width:99%;white-space: nowrap">
          <span class='center' style="text-align:center;margin:5px;background-color:red;align=center" ng-repeat='d in vm.data'>{{d}}</span>
       </ion-scroll>
        </div>