2012-03-21 123 views
0

嗨我在我的應用程序中有一個畫廊,當您單擊縮略圖時在圖像視圖中顯示圖像。我正在爲一個警告對話框設置一個長單擊偵聽器,該對話框有2個按鈕,一個用於設置壁紙,另一個用於分享。我有共享意圖和獲取圖形緩存有點工作。它適用於模擬器,但不適用於我的手機。我已經在這個網站上使用了很多例子來實現這個目標,但是它根本不工作。它保持關閉我的手機上的應用程序的力量。任何幫助表示讚賞。Android只讀文件系統錯誤

       alertDialog.setButton2("Share", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 

          imgView.setDrawingCacheEnabled(true); 
          Bitmap b = imgView.getDrawingCache(); 
          File sdCard = Environment.getExternalStorageDirectory(); 
          File file = new File(sdCard.getAbsolutePath() + "image.jpg"); 

          try { 
           file.createNewFile(); 
           OutputStream fos = null; 
           fos = new FileOutputStream(file); 
           b.compress(CompressFormat.JPEG, 100, fos); 
           fos.flush(); 
           fos.close(); 
          } catch (FileNotFoundException e) { 

           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 

          Log.d("Save Example", "Image saved."); 

          Intent shareIntent = new Intent(Intent.ACTION_SEND); 
          Uri phototUri = Uri.parse("file:///sdcard/image.jpg"); 
          shareIntent.setData(phototUri); 
          shareIntent.setType("image/jpeg"); 
          shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri); 
          startActivity(Intent.createChooser(shareIntent, "Share Via")); 
         } 

        }); 

      alertDialog.show(); 
      return true; 
     } 
    }); 

更新:它似乎是一個問題,

b.compress(CompressFormat.JPEG, 95, fos); 

口口聲聲說空指針。

原來,空指針實際上是一個read_only錯誤。所以它沒有實際寫入文件,我得到一個ioexception只讀文件系統。它爲什麼這樣做?

+0

在嘗試寫入之前,您需要查看目錄的權限。您必須瞭解UNIX的基本文件系統權限才能執行此分析。 – JoxTraex 2012-03-21 05:49:41

+0

你在清單文件中使用寫入權限? – 2012-03-21 05:54:07

+0

是的,我在清單中有write.external.storage – Pestilence 2012-03-21 14:30:59

回答

0

好的,這裏是我最終做的,以獲得這個分享照片,以防萬一別人需要它。事實證明,我的ImageView沒有被渲染成BitMap,所以它沒有任何文件。所以相反,我只是從它的位置調用drawable並保存它。它爲這個簡單的畫廊提供了更多清潔的代碼。我有一個警告對話框,但它也可以設置爲常規按鈕。

   alertDialog.setButton2("Share", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int which) { 

          long currentTime = System.currentTimeMillis(); 
          imgView.setDrawingCacheEnabled(true); 
          String newFolder = "/CFW"; 
          String extStorageDirectory = Environment 
            .getExternalStorageDirectory() 
            .toString(); 
          File sdCard = new File(extStorageDirectory 
            + newFolder); 
          sdCard.mkdir(); 

          File file = new File(sdCard.getAbsolutePath() 
            + "/cfw" + currentTime + ".jpg"); 

          try { 
           InputStream is = getResources() 
             .openRawResource(pics[position]); 
           OutputStream os = new FileOutputStream(file); 
           byte[] data = new byte[is.available()]; 
           is.read(data); 
           os.write(data); 
           is.close(); 
           os.close(); 
          } catch (FileNotFoundException e) { 

           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 

          Log.d("Save Example", "Image saved."); 

          Intent shareIntent = new Intent(
            Intent.ACTION_SEND); 
          Uri photoUri = Uri 
            .parse("file:///sdcard/CFW/cfw" 
              + currentTime + ".jpg"); 
          shareIntent.setData(photoUri); 
          shareIntent.setType("image/jpeg"); 
          shareIntent.putExtra(Intent.EXTRA_STREAM, 
            photoUri); 
          startActivity(Intent.createChooser(shareIntent, 
            "Share Via")); 
         } 

        }); 

      alertDialog.show(); 
      return true; 
     } 
    }); 

} 
相關問題