1

If I save my image using the below code, and then open my gallery manually, the image displays fine. If I try to open the gallery programmatically, I get a message from the gallery saying "No Thumbnail".

//save bitmap--------------------------------------------------
                File myDir=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"");
                myDir.mkdirs();
                Random generator = new Random();
                int n = 10000;
                n = generator.nextInt(n);
                String fname = "Image-"+ n +".jpg";
                File file = new File (myDir, fname);
                if (file.exists ()) file.delete ();
                try {
                    FileOutputStream out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    out.flush();
                    out.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                //media scan--------------------------------------------------
                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                File f = new File(myDir.getAbsolutePath());
                Uri contentUri = Uri.fromFile(f);
                mediaScanIntent.setData(contentUri);
                this.getApplicationContext().sendBroadcast(mediaScanIntent);

                //view in gallery (here is where my gallery gives "No Thumbnail" message)----
                Intent viewIntent = new Intent();
                viewIntent.setAction(Intent.ACTION_VIEW);
                viewIntent.setDataAndType(Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+""),"image/*");
                startActivity(viewIntent);

no thumbnail

CptRayMar
  • 133
  • 4
  • 19
  • I'm gonna guess here. You have to wait the MediaScanner to finish to then open the gallery. Use the https://developer.android.com/reference/android/media/MediaScannerConnection.html `scanFile` method that have the callback option when the MediaScanner completed. (ps.: I also this broadcast method will not work in KitKat) – Budius Mar 05 '14 at 22:46
  • @Budius If this isnt going to work in KitKat then I'm going to have to find another option of getting it into gallery :/ Thanks for your help – CptRayMar Mar 05 '14 at 23:17
  • I implemented this instead of the broadcast http://stackoverflow.com/a/19867466/1298509 and opened the gallery in the onScanCompleted method, I still get the thumbnail problem. I also tried implementing a second button to launch the gallery, and waited a long while to press it (giving the scanner time). No help. I haven't seen anyone else get this. Maybe it's my phone? – CptRayMar Mar 05 '14 at 23:28

0 Answers0