2014-02-26 99 views
1

我試圖打開一個文件以代表該途徑作爲一個參數字符串加載從SD卡/設備存儲器中的圖像:FileNotFoundException異常加載圖像

File imagePathFile = new File(savedImapthPathwayOnPause); 
     try { 

      final int IMAGE_MAX_SIZE = 3000; 
      FileInputStream streamIn = new FileInputStream(imagePathFile); 

      // important to reduce size of image before it is loaded into memory 
      // and then resized, otherwise will 
      // get out of memory error 

      // Decode image size and setInJBounds = true to avoid auto memory 
      // allocation for large image 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(streamIn, null, o); 
      streamIn.close(); 

      //by setting m.pow(scale, 3.85) we get a org image of 2500:1400 beofre loaded and when loaded we get 426:240, no need fro resize method call here 
      int scale = 1; 
      while ((o.outWidth * o.outHeight) * (1/Math.pow(scale, 3.73)) > IMAGE_MAX_SIZE) { 
       scale++; 
      } 

      // get orginal width of image before loaded into memory 
      Log.d("VIEW DIVE", "scale = " + scale + ", orig-width: " + o.outWidth 
        + " orig-height: " + o.outHeight); 
      //Toast.makeText(getApplicationContext(), "Orginal Image size from DB: width: "+ o.outWidth + "/height: " + o.outHeight , Toast.LENGTH_LONG).show(); 

      Bitmap b = null; 
      streamIn = new FileInputStream(imagePathFile); 
      if (scale > 1) { 
       scale--; 
       // scale to max possible inSampleSize that still yields an image 
       // larger than target, inSampleSize loads the image into memor 
       // by a factor of its integer value 
       o = new BitmapFactory.Options(); 
       o.inSampleSize = scale; 
       // Decode bitmap with inSampleSize set 
       o.inJustDecodeBounds = false; 

       b = BitmapFactory.decodeStream(streamIn, null, o); 
       //makeToast("Image after sacling and now loading: width: "+ o.outWidth + "/height: " + o.outHeight); 
       //resizedImage = reSizeImage(b); this is blowing uo and alreadt samll image from 
       //org size 2500:1400 to 73:44, then blown back up in resize does not look good 

       streamIn.close(); 
       //b.recycle(); 
       System.gc(); 

但我不斷收到以下例外:

java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory) 
    at libcore.io.IoBridge.open(IoBridge.java:416) at 
java.io.FileInputStream.<init>(FileInputStream.java:78)  at 
com.mooney.diveapp.LogDive.setImageViewFromSavedInstance(LogDive.java:198) 
    at com.mooney.diveapp.LogDive.onCreate(LogDive.java:134) at 
android.app.Activity.performCreate(Activity.java:5255)  at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 

編輯:圖像路徑字符串如何設置要求(在asycnh內部類完成):

// check if external storage is available to write 
      Bitmap imageToSave = params[0];// get image 

      final String state = Environment.getExternalStorageState(); 
      if (Environment.MEDIA_MOUNTED.equals(state)) { 
       // media available 
       canSaveToExternalSD = true; 
       // use a random number to append to image file name until use 
       // diveNumber according to log entry on 
       Random diveNum = new Random(); 
       File sdCard = Environment.getExternalStorageDirectory(); 
       final File directory = new File(sdCard.getAbsolutePath() 
         + File.separator + diveAlbum); 
       final File theFile = new File(directory, "image" 
         + diveNum.nextInt(5000) + ".png"); 
       directory.getParentFile().mkdirs(); 

       if (!directory.mkdirs()) { 
        Log.d(TAG, "Directory not created!!!!"); 
       } 

       try { 
        FileOutputStream out = new FileOutputStream(theFile); 
        imageToSave.compress(Bitmap.CompressFormat.PNG, 90, out); 
        out.flush(); 
        out.close(); 

//assign path to a string 
savedImagePath = theFile.getAbsolutePath(); 

稍後在代碼中將saveImagePath分配給savedImapthPathwayOnPause。

編輯:稍後在代碼中展開。 因此,當用戶通過調用活動B中的Goggle Maps或Facebookpost活動來離開窗體活動A時,我將字符串savedImagePath保存在onSavedINstance狀態中。

然後,當用戶返回太活動A我得到savedIMagePathway創建,並將其傳遞到上面張貼的方法文件imagePathFile =新文件(savedImagePathwayOnPause)上面貼出。

此時的savedImagePathwayOnPause的值是:/storage/emulated/0/dive_photos/image2588.png

但是,有時該值返回空(到其初始化狀態=「」)。這似乎是隨機的......因爲有些時候的作品,而不是別人......

@Override 
     protected void onSaveInstanceState(Bundle outState) { 

      //if onPause called on activity when user selects maps or FB, we lose value of global varibles like 
      //saveIMageBPath and photoBitMap but keep all view values (Edittexts) 
      //so we must save our global values here and call them back on onRestoreInstance state and assign to IMageButton 

      super.onSaveInstanceState(outState); 
      uiHelper.onSaveInstanceState(outState); 


      outState.putString("savedImapthPathwayOnSaveInstance", savedImagePath); 
     } 

和的onCreate():

if (savedInstanceState != null) { 
      // Restore value of members from saved state 
      // mCurrentScore = savedInstanceState.getInt(STATE_SCORE); 
      this.savedImapthPathwayOnPause = savedInstanceState.getString("savedImapthPathwayOnSaveInstance"); 
      Toast.makeText(getApplicationContext(), "on returning the image path is : " + savedImapthPathwayOnPause, Toast.LENGTH_SHORT).show(); 
      //no we ust get the bitmap form the image path and return to IMageView 
      Log.d("MYTAG", "on returning the image path is : " + savedImapthPathwayOnPause); 

      if(!savedImapthPathwayOnPause.isEmpty()) 
      setImageViewFromSavedInstance(savedImapthPathwayOnPause); 
      Log.d("MYTAG", "on returning the image path not found:== " + savedImapthPathwayOnPause); 
     } 
+1

例外表明您試圖打開根目錄下,有可能是因爲你提供了一個有效的空(甚至空?)路徑。記錄你的路徑字符串的實際是什麼,並找出它爲什麼不是你所期望的。該問題不在發佈的代碼中。 –

+1

「稍後在代碼中」不會提供太多信息,特別是當您說有異步代碼在播放時。如果選擇文件路徑的代碼與使用它的代碼並行運行,則可能會發現自己試圖在分配之前使用'savedImapthPathwayOnPause' - 這可以解釋問題。如果不是這種情況,則應在其上下文(類,函數等)中發佈所有相關代碼 – PaF

+0

hmmmm問題是同步任務需要幾秒鐘才能設置savedImagePathwayOnPause ...如果之前調用另一個活動這是設置一個空字符串傳遞給文件.... – dancingbush

回答

0

由於@Paf提出的問題是圖像路徑已經被在同步類中生成,因此如果在生成路徑之前調用File open,則會將空字符串傳遞給File構造函數。

爲了避免這種情況,我禁用了任何對創建文件的調用,方法是禁用在Pre Excevent中這樣做的按鈕,然後在生成字符串文件路徑後,再次在Aynsch內部類的onPostExceute方法中啓用它們。

感謝

相關問題