Android将图像保存在SD卡中

时间:2014-03-04 05:32:37

标签: android image save gallery android-external-storage

我将图像保存到SD卡中,但我希望目录文件夹会自动显示在图库中,文件夹上的图像也会显示。每当我保存图像时,我都会重新启动手机,以便在库中显示目录文件夹。是我的代码有问题吗?还是电话?请帮我。非常感谢。我不知道该怎么做

这是我的代码:

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

3 个答案:

答案 0 :(得分:0)

试试这个:

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

不要忘记添加存储空间权限

由于这是将数据保存在外部存储器上的操作,因此需要AndroidManifest.xml权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:0)

尝试一下

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

并在清单文件中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 2 :(得分:0)

问题不在于代码...

这里发生的事情是
在SD卡上下载文件后,不会通过添加或下载到系统的新文件通知图库
你需要做的是你必须手动通知画廊...添加了okhay画廊文件请显示.. :)

为此你必须使用MediaScannerConnection

下载文件,扫描特定文件,它将显示在图库中
你完成了:)