将文件保存到android库并扫描它

时间:2014-11-20 13:25:49

标签: android animated-gif android-file

我正在尝试将gif图片下载到gallery

我的代码与pngjpg

完美配合

我的代码不适用于任何gif

java.lang.NullPointerException at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:648)`

这是我的代码

private void saveFile(Bitmap bitmap, String name) {

    String filename = "XXX";
    ContentValues values = new ContentValues();
    File sdImageMainDirectory = new File(Environment
            .getExternalStorageDirectory(), "XXX");
    sdImageMainDirectory.mkdirs();
    File outputFile = new File(sdImageMainDirectory, filename);
    values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
    values.put(MediaStore.MediaColumns.TITLE, filename);
    values.put(MediaStore.MediaColumns.DATE_ADDED, System
            .currentTimeMillis());
    values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
    values.put(MediaStore.MediaColumns.MIME_TYPE, "image/gif");
    Uri uri = mActivity.getContentResolver().insert(
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
    try {
        OutputStream outStream = mActivity.getContentResolver()
                .openOutputStream(uri);
        bitmap.compress(Bitmap.CompressFormat.PNG, 95, outStream);

        outStream.flush();
        outStream.close();


        String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + filename;    
        MediaScannerConnection.scanFile(mActivity, new String[] { filePath }, null, null);

    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.v("ExternalStorage", "Error writing " , e);
    }
}

这是日志

11-20 15:14:26.010: E/AndroidRuntime(24838): FATAL EXCEPTION: Thread-32366
11-20 15:14:26.010: E/AndroidRuntime(24838): java.lang.NullPointerException
11-20 15:14:26.010: E/AndroidRuntime(24838):    at     android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:648)
11-20 15:14:26.010: E/AndroidRuntime(24838):    at android.content.ContentResolver.openOutputStream(ContentResolver.java:540)
11-20 15:14:26.010: E/AndroidRuntime(24838):    at android.content.ContentResolver.openOutputStream(ContentResolver.java:516)
11-20 15:14:26.010: E/AndroidRuntime(24838):    at com.xx.www.MediaFragment.saveFile(MediaFragment.java:2238)
11-20 15:14:26.010: E/AndroidRuntime(24838):    at com.xxx.www.MediaFragment.access$1(MediaFragment.java:2218)
11-20 15:14:26.010: E/AndroidRuntime(24838):    at com.xxx.www.MediaFragment$14$1.run(MediaFragment.java:1849)
11-20 15:14:26.010: E/AndroidRuntime(24838):    at java.lang.Thread.run(Thread.java:841)

我做错了什么?

0 个答案:

没有答案