سؤال

I am writing an Android app, and trying to save bitmap into external storage. My code is as follows.

        ContentValues contents = new ContentValues();
        String curTime = AppSetting.currentDateString();
        contents.put("title", curTime);         
        contents.put("display_name", curTime);
        contents.put("description", curTime);
        contents.put("datetaken", Long.valueOf(System.currentTimeMillis()));
        contents.put("mime_type", "image/jpeg");
        String path = Environment.getExternalStorageDirectory().toString() + "/dcim/camera";
        String hashPath = String.valueOf(path.hashCode());
        String displayName = (new File(path)).getName().toLowerCase();
        contents.put("bucket_id", hashPath);
        contents.put("bucket_display_name", displayName);
        Uri uri = this.getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, contents);//this is always null!!!
        BufferedOutputStream oStream = new BufferedOutputStream(this.getContentResolver().openOutputStream(uri));
        bmp.compress(CompressFormat.JPEG, 100, oStream);
        oStream.close();

But uri is always null. In doing some research, I found sources that suggest it is because activity was destroyed and recreated. To address this I added above code into Activity's OnCreate(), but I still get the same result.
I tested with several Android devices, but uri is always null. Any advice?

هل كانت مفيدة؟

المحلول

I found my problem.
I replaced display name with following macro and it worked.

MediaStore.Images.ImageColumns.DISPLAY_NAME

As a result I found this is not "display_name", it has underbar prefix: "_display_name".
Hope this helps.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top