2014-03-04 68 views
0

我正在將圖像保存到SD卡中,但我希望目錄文件夾將自動顯示在圖庫和文件夾中的圖像上。每當我保存圖像時,我都會重新啓動手機,以便在圖庫中顯示目錄文件夾。是我的代碼有問題嗎?或電話?請幫幫我。非常感謝。我不知道該怎麼辦Android將圖像保存在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()); 
     } 
+0

[圖像,保存到SD卡,沒有出現在Android的畫廊應用](http://stackoverflow.com/問題/ 2170214/image-saved-to-sdcard-doesnt-appear-in-androids-gallery-app) – Squonk

+0

保存圖像時沒有重複:(我不知道爲什麼。你能幫我麼? – user3317993

回答

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

,並在您maniefest文件

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

仍然我沒有自動顯示在畫廊:(我將如何做? – user3317993

+0

你看到@ Squonk的鏈接..http://stackoverflow.com/questions/2170214/image-saved-to-sdcard-doesnt-出現在androids-gallery-app – rajshree

+0

文件文件; // =你的文件 String mimetype; // =你的文件的mimetype MimeTypeMap可能是有用的 MediaScanner.scanFile(getApplicationContext(),new String [] {file。 getPath()},new String [] {m​​imetype},null); ..,看到這個 – rajshree

0

的問題是不是與代碼添加權限......

什麼這裏發生過的
在SD卡上下載文件後,畫廊不會通知添加或下載到系統的新文件
你需要做的是,你必須手動通知...... okhay庫文件添加請出示畫廊.. :)

對於您必須使用MediaScannerConnection

下載的文件,掃描該特定的文件,它會顯示在畫廊
,你就完成了:)

+0

我將如何做圖庫文件的通知?你有任何樣品嗎?請幫助我謝謝你:D – user3317993