将文件保存到外部存储器时出现FileNotFoundException

时间:2015-07-28 01:33:21

标签: android image bitmap gallery filenotfoundexception

我有一个按钮,可以使用Picasso库将图像下载到手机库..图像已下载,但我无法将其保存到图库中..

我正在尝试使用以下代码将图像保存到DCIM文件夹:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0xffffffff addr=0x0 pc=0x78a77]

goroutine 1 [running]:
crypto/tls.(*Conn).Handshake(0x0, 0x1050a008, 0x0, 0x0)
    /usr/src/go/src/crypto/tls/conn.go:967 +0x317
crypto/tls.(*Conn).Write(0x0, 0x10538120, 0x3, 0x8, 0x0, 0x0, 0x0, 0xfefd2220)
    /usr/src/go/src/crypto/tls/conn.go:845 +0x80
main.main()
    /tmp/sandbox290881341/main.go:49 +0x1c0

goroutine 2 [runnable]:
runtime.forcegchelper()
    /usr/src/go/src/runtime/proc.go:90
runtime.goexit()
    /usr/src/go/src/runtime/asm_amd64p32.s:1086 +0x1

goroutine 3 [runnable]:
runtime.bgsweep()
    /usr/src/go/src/runtime/mgc0.go:82
runtime.goexit()
    /usr/src/go/src/runtime/asm_amd64p32.s:1086 +0x1

goroutine 4 [runnable]:
runtime.runfinq()
    /usr/src/go/src/runtime/malloc.go:712
runtime.goexit()
    /usr/src/go/src/runtime/asm_amd64p32.s:1086 +0x1

但我得到以下例外:

  

java.io.FileNotFoundException:   /storage/sdcard/DCIM/offer11-img1438046842226.jpg:打开失败:EACCES   (没有权限)   引起:libcore.io.ErrnoException:打开失败:EACCES(权限被拒绝)

在这一行:

                if (bitmap != null) {
                    FileOutputStream out = null;
                    try {

                        String name = Environment
                                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                                + "/"
                                + pageTitle
                                + "-img"
                                + System.currentTimeMillis() + "." + imgExt;

                        Log.d("Bitmap", "Name: " + name);

                        File f = new File(name);
                        out = new FileOutputStream(f);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);


                        Intent mediaScanIntent = new
                        Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                        Uri contentUri = Uri.fromFile(f);
                        mediaScanIntent.setData(contentUri);
                        SinglePageActivity.this.sendBroadcast(mediaScanIntent);

                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (out != null) {
                                out.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    Log.d("Bitmap", "Null image");
                }

还有更有效的方法将图像直接保存到图库吗?

由于

2 个答案:

答案 0 :(得分:0)

检查您是否已在

中声明了权限
    AndroidManifest.xml

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>,

如果没有,则尝试添加;

答案 1 :(得分:0)

你可以这样使用

public class ImageDownload implements Target {

    public static final String TAG = "tag";

    private String name;

    public ImageDownload(String fileName) {

        name = fileName.substring(fileName.lastIndexOf("/"));

    }

    @Override
    public void onBitmapFailed(Drawable arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onBitmapLoaded(final Bitmap bitmap, LoadedFrom arg1) {
        Log.e("ImageDownlaod", "inside Download ");

        new Thread(new Runnable() {
            @Override
            public void run() {

                File file = new File(Environment.getExternalStorageDirectory()
                        .getPath() + "/" + FACTOR_APP + "/" + name);
                try {
                    file.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(file);

                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
                    ostream.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    @Override
    public void onPrepareLoad(Drawable arg0) {
        // TODO Auto-generated method stub

    }



}

您可以将此课程用作

public void downLoadImagesWithPicasso() {

                Picasso.with(this).load(thumbnailPath)
                        .into(new ImageDownload(thumbnailPath));


    }