创建新文件夹以存储下载的图像

时间:2014-07-04 19:18:25

标签: android file

我正在创建一个应用程序,从网址下载图像,然后在图片视图中显示。 之后,它检查外部SD卡上是否有任何名称为 vedaImages 的文件夹,如果此类文件夹存在,则图像被压缩并保存在其中,否则将创建具有该名称的文件夹并压缩和保存图像。

所以在ImageView中显示它工作得很好但是没有创建文件夹。 这是onrecieve方法的代码。

    private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(preferenceManager.getLong(strPref_Download_Id,
                0));
        Cursor cursor = downloadManager.query(query);
        if (cursor.moveToFirst()) {
            int columnIndex = cursor
                    .getColumnIndex(DownloadManager.COLUMN_STATUS);
            int status = cursor.getInt(columnIndex);
            if (status == DownloadManager.STATUS_SUCCESSFUL) {
                long downloadId = preferenceManager.getLong(
                        strPref_Download_Id, 0);
                ParcelFileDescriptor file;
                try {
                    file = downloadManager.openDownloadedFile(downloadId);
                    FileInputStream fileInputStream = new ParcelFileDescriptor.AutoCloseInputStream(
                            file);
                    Bitmap bm = BitmapFactory.decodeStream(fileInputStream);
                    imgview.setImageBitmap(bm);

                    File f = new File(
                            Environment.getExternalStorageDirectory()
                                    + "/vedaImages");

                    if (f.isDirectory()) {

                        File fileWithinMyDir = new File(f, "myfile");
                        FileOutputStream out = new FileOutputStream(
                                fileWithinMyDir);
                        bm.compress(Bitmap.CompressFormat.JPEG, 85, out);

                    } else {
                        File directory = new File(
                                Environment.getExternalStorageDirectory()
                                        + File.separator + "vedaImages");
                        directory.mkdirs();

                        File fileWithinMyDir = new File(directory, "myfile");
                        FileOutputStream out = new FileOutputStream(
                                fileWithinMyDir);
                        bm.compress(Bitmap.CompressFormat.JPEG, 85, out);

                    }

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}; 

按下“下载”按钮后继承了logcat。

   07-05 02:40:34.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 55907/7
07-05 02:40:36.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 55909/7
07-05 02:40:37.726: D/AndroidRuntime(15857): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-05 02:40:37.726: D/AndroidRuntime(15857): CheckJNI is OFF
07-05 02:40:37.976: D/AndroidRuntime(15857): Calling main entry com.android.commands.pm.Pm
07-05 02:40:37.986: D/AndroidRuntime(15857): Shutting down VM
07-05 02:40:37.996: D/dalvikvm(15857): Debugger has detached; object registry had 1 entries
07-05 02:40:37.996: I/AndroidRuntime(15857): NOTE: attach of thread 'Binder Thread #3' failed
07-05 02:40:38.356: D/AndroidRuntime(15870): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-05 02:40:38.366: D/AndroidRuntime(15870): CheckJNI is OFF
07-05 02:40:38.616: D/AndroidRuntime(15870): Calling main entry com.android.commands.am.Am
07-05 02:40:38.626: I/ActivityManager(223): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.ved_download.download/.DownloadMainActivity} from pid 15870
07-05 02:40:38.706: D/AndroidRuntime(15870): Shutting down VM
07-05 02:40:38.726: D/ViewRootImpl(223): pckname = com.ved_download.download
07-05 02:40:38.726: D/dalvikvm(15870): Debugger has detached; object registry had 1 entries
07-05 02:40:38.736: I/AndroidRuntime(15870): NOTE: attach of thread 'Binder Thread #3' failed
07-05 02:40:38.786: D/OpenGLRenderer(402): Flushing caches (mode 1)
07-05 02:40:38.856: D/OpenGLRenderer(402): Flushing caches (mode 0)
07-05 02:40:38.956: D/ViewRootImpl(15434): pckname = com.ved_download.download
07-05 02:40:39.006: D/ViewRootImpl(15434): pckname = com.ved_download.download
07-05 02:40:39.226: I/Process(223): Sending signal. PID: 15434 SIG: 3
07-05 02:40:39.226: I/dalvikvm(15434): threadid=3: reacting to signal 3
07-05 02:40:39.236: I/dalvikvm(15434): Wrote stack traces to '/data/anr/traces.txt'
07-05 02:40:39.296: I/ActivityManager(223): Displayed com.ved_download.download/.DownloadMainActivity: +581ms
07-05 02:40:40.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 55929/7
07-05 02:40:45.406: I/DownloadManager(11827): Initiating request for download 87
07-05 02:40:45.486: D/ViewRootImpl(308): pckname = com.android.systemui
07-05 02:40:46.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 55948/7
07-05 02:40:48.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 55950/7
07-05 02:40:52.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 55953/7
07-05 02:40:54.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 55969/7
07-05 02:40:56.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 55971/7
07-05 02:41:00.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 55990/7
07-05 02:41:06.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 56011/7
07-05 02:41:09.036: I/qtaguid(11827): Failed write_ctrl(u 70) res=-1 errno=22
07-05 02:41:09.036: I/qtaguid(11827): Untagging socket 70 failed errno=-22
07-05 02:41:09.036: W/NetworkManagementSocketTagger(11827): untagSocket(70) failed with errno -22
07-05 02:41:09.416: W/CursorWrapperInner(15434): Cursor finalized without prior close()
07-05 02:41:09.536: W/System.err(15434): java.io.FileNotFoundException: /mnt/sdcard/vedaImages/myfile: open failed: ENOENT (No such file or directory)
07-05 02:41:09.536: W/System.err(15434):    at libcore.io.IoBridge.open(IoBridge.java:406)
07-05 02:41:09.536: W/System.err(15434):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
07-05 02:41:09.536: W/System.err(15434):    at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
07-05 02:41:09.536: W/System.err(15434):    at com.ved_download.download.DownloadMainActivity$1.onReceive(DownloadMainActivity.java:110)
07-05 02:41:09.536: W/System.err(15434):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
07-05 02:41:09.536: W/System.err(15434):    at android.os.Handler.handleCallback(Handler.java:605)
07-05 02:41:09.536: W/System.err(15434):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-05 02:41:09.536: W/System.err(15434):    at android.os.Looper.loop(Looper.java:137)
07-05 02:41:09.536: W/System.err(15434):    at android.app.ActivityThread.main(ActivityThread.java:4424)
07-05 02:41:09.536: W/System.err(15434):    at java.lang.reflect.Method.invokeNative(Native Method)
07-05 02:41:09.536: W/System.err(15434):    at java.lang.reflect.Method.invoke(Method.java:511)
07-05 02:41:09.536: W/System.err(15434):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-05 02:41:09.546: W/System.err(15434):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-05 02:41:09.546: W/System.err(15434):    at dalvik.system.NativeStart.main(Native Method)
07-05 02:41:09.546: W/System.err(15434): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-05 02:41:09.546: W/System.err(15434):    at libcore.io.Posix.open(Native Method)
07-05 02:41:09.546: W/System.err(15434):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-05 02:41:09.546: W/System.err(15434):    at libcore.io.IoBridge.open(IoBridge.java:390)
07-05 02:41:09.546: W/System.err(15434):    ... 13 more
07-05 02:41:12.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 56030/7
07-05 02:41:18.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 56051/7
07-05 02:41:24.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 56070/7
07-05 02:41:28.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 56072/7
07-05 02:41:30.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 56084/7
07-05 02:41:32.646: D/GSMMUX(216): 3005:main(): Frames received/dropped: 56115/7

1 个答案:

答案 0 :(得分:0)

将您的代码更改为:

File file = new File(Environment.getExternalStorageDirectory() + "/vedaImages/");

if(!file.exists()){
  file.mkdirs();
}

还要确保您拥有此权限:

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