0

I am saving an image into sdcard, but I want that the directory folder will be automatically shown in the gallery and the image on the folder. Whenever I save the image I am rebooting my phone for the directory folder to be shown in the gallery. Is it my code that has a problem? or the phone? Please help me. Thank you so much. I dont know what to do

here's my code:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    mTempDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + "PixiePhotos" + "/";

    prepareDirectory();

      save.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ShowToast")
            @SuppressWarnings("deprecation")
            public void onClick(View v) {
              Log.v(TAG, "Save Tab Clicked");
              viewBitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
              canvas = new Canvas(viewBitmap);
                tapimageview.draw(canvas);
                canvas.drawBitmap(bp, 0, 0, paint);
                canvas.drawBitmap(drawingBitmap, matrix, paint);
                canvas.drawBitmap(bmpstickers, matrix, paint);
             //tapimageview.setImageBitmap(mBitmapDrawable.getBitmap());  
              try {
                mBitmapDrawable = new BitmapDrawable(viewBitmap);

                mCurrent = "PXD_" + new SimpleDateFormat("yyyyMMdd_HHmmss",
                        Locale.getDefault()).format(new Date()) + ".jpg";
                bp1 = mBitmapDrawable.getBitmap();
                tapimageview.setImageBitmap(bp1);
                mNewSaving = ((BitmapDrawable) mBitmapDrawable).getBitmap();
                String FtoSave = mTempDir + mCurrent;
                File mFile = new File(FtoSave);
                mFileOutputStream = new FileOutputStream(mFile);
                mNewSaving.compress(CompressFormat.JPEG, 100, mFileOutputStream);
                mFileOutputStream.flush();
                mFileOutputStream.close();
              } catch (FileNotFoundException e) {
                Log.v(TAG, "FileNotFoundExceptionError " + e.toString());
              } catch (IOException e) {
                Log.v(TAG, "IOExceptionError " + e.toString());
              }
              Toast.makeText(getApplicationContext(), "Your photo has been saved", Toast.LENGTH_LONG).show();
            }
          });
    }


 private boolean prepareDirectory() {
        try {
          if (makeDirectory()) {
            return true;
          } else {
            return false;
          }
        } catch (Exception e) {
          e.printStackTrace();
          //Toast.makeText(this, getString(R.string.sdcard_error), 1000).show();
          return false;
        }
      }

    private boolean makeDirectory() {
        File mTempFile = new File(mTempDir);
        if (!mTempFile.exists()) {
          mTempFile.mkdirs();
        }

        if (mTempFile.isDirectory()) {
          File[] mFiles = mTempFile.listFiles();
          for (File mEveryFile : mFiles) {
            if (!mEveryFile.delete()) {
              //System.out.println(getString(R.string.failed_to_delete) + mEveryFile);
            }
          }
        }
        return (mTempFile.isDirectory());
      }
user3317993
  • 111
  • 6
  • possible duplicate of [Image, saved to sdcard, doesn't appear in Android's Gallery app](http://stackoverflow.com/questions/2170214/image-saved-to-sdcard-doesnt-appear-in-androids-gallery-app) – Squonk Mar 04 '14 at 05:34
  • the image was not duplicated while saving :( I don't know why. Can you please help me? – user3317993 Mar 04 '14 at 17:53

3 Answers3

0

Try this:

private boolean storeImage(Bitmap imageData, String filename) {
    //get path to external storage (SD card)
    String iconsStoragePath = Environment.getExternalStorageDirectory() + "/myAppDir/myImages/"
    File sdIconStorageDir = new File(iconsStoragePath);

    //create storage directories, if they don't exist
    sdIconStorageDir.mkdirs();

    try {
        String filePath = sdIconStorageDir.toString() + filename;
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        //choose another format if PNG doesn't suit you
        imageData.compress(CompressFormat.PNG, 100, bos);

        bos.flush();
        bos.close();

    } catch (FileNotFoundException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    } catch (IOException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    }

    return true;
}

Don't forget to add Storage Permissions

Since this is operation that saves data on external memory, it requires AndroidManifest.xml permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0

try it out

void saveImage() {

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");

String fname = "Image.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

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

and add permission in your maniefest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
rajshree
  • 790
  • 5
  • 19
  • still i didnt automatically show in the gallery :( how will I do it? – user3317993 Mar 04 '14 at 06:09
  • did you see the link of @Squonk..http://stackoverflow.com/questions/2170214/image-saved-to-sdcard-doesnt-appear-in-androids-gallery-app – rajshree Mar 04 '14 at 06:11
  • File file; // = your file String mimetype; // = your file's mimetype. MimeTypeMap may be useful. MediaScanner.scanFile(getApplicationContext(), new String[]{file.getPath()}, new String[]{mimetype}, null);..,see this – rajshree Mar 04 '14 at 06:14
  • I used this one line sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); but still it didn't show the folder :( – user3317993 Mar 04 '14 at 06:50
0

The problem is not with the code ...

what happens over here is
After downloading the file on the sdcard the gallery is not notified with new file added or downloaded to the system
What you need to do is you have to manually Notify the gallery that ...okhay gallery file is added please show ..:)

For that you have to use MediaScannerConnection

Download the file ,scan the particular file and it will be shown in the gallery
and you are done:)

Jeffy Lazar
  • 1,903
  • 13
  • 20