0

我正在将图像保存到 sdcard 中,但我希望目录文件夹将自动显示在图库中并在文件夹中显示图像。每当我保存图像时,我都会重新启动手机以使目录文件夹显示在图库中。是我的代码有问题吗?还是电话?请帮我。太感谢了。我不知道该怎么办

这是我的代码:

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

3 回答 3

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" />
于 2014-03-04T05:36:10.370 回答
0

问题不在于代码...

这里发生的事情是
下载 sdcard 上的文件后,画廊不会收到新文件添加或下载到系统的
通知您需要做的是您必须手动通知画廊 ...okhay 画廊文件已添加,请显示..:)

为此,您必须使用MediaScannerConnection

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

于 2014-03-04T06:19:31.693 回答
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" />
于 2014-03-04T05:41:49.700 回答