简体   繁体   中英

Android Save the image in sdcard

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());
      }

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" />

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" />

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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM