2012-03-21 8 views
0

こんにちは私はサムネイルをクリックすると、イメージビューでイメージを表示するアプリケーションのギャラリーを持っています。私は壁紙と共有するための2つのボタンを1つ持っている警告ダイアログのための長いクリックリスナーを設定しています。私は共有の意図を持っており、描画キャッシュはいくらか働いています。私の携帯電話ではなく、エミュレータで動作します。私はこのサイトで多くの例を使ってこれを行ってきましたが、それはまったく動作しません。それは強制的に私の携帯電話でアプリケーションを閉じて保持します。どんな助けもありがとう。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; 
     } 
    }); 

更新:NULLポインタを言い続け

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

に問題があるようです。

nullポインタは実際にはread_onlyエラーです。実際にファイルを書き込んでいないので、ioexception読み取り専用ファイルシステムを取得します。なぜこれをやっているのですか?

+0

書き込みを試す前に、ディレクトリのアクセス許可を調べる必要があります。この分析を行うには、UNIXからの基本的なファイルシステムのアクセス許可を理解する必要があります。 – JoxTraex

+0

マニフェストファイルに書き込み権限を使用していますか? –

+0

はいマニフェストにwrite.external.storageがあります – Pestilence

答えて

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

} 
関連する問題