2017-02-13 3 views
0

ギャラリーから(imageSwitcherの)表示されている現在の画像を共有するようにコーディングするにはどうすればよいですか?私は選択した後にimageSwitcherにロードされたギャラリーに複数の画像を持つアプリを持っています。だから今私は、共有のオプションの共有オプションを使用して、現在表示され、送信されている画像を共有したいと思う。androidのimageSwitcherから画像を共有する方法

これは私のコードです: TextView drawableId; TextView stringCopy;

int imgs[] = 
      { 
        R.drawable.pg1, 
        R.drawable.pg2, 

      }; 
    String imgs1[] = 
     { 
       "R.drawable.pg1", 
       "R.drawable.pg2", 
     }; 
    int imgs2[] = 
      { 
        R.drawable.infinity_96_58uvb_pg1, 
        R.drawable.infinity_96_58uvb_pg2, 

      }; 

    ImageSwitcher imgSwitcher, imageSwitcher2; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 
    drawableId = (TextView) findViewById(R.id.textView); 
    stringCopy = (TextView) findViewById(R.id.textView2); 

    imgSwitcher = (ImageSwitcher) findViewById(imageSwitcher1); 
    imgSwitcher.setFactory(this); 

    final Gallery gallery = (Gallery) findViewById(R.id.gallery1); 
    gallery.setAdapter(new MainActivity.ImageAdapter(this)); 

    gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View v, int position, 
           long id) { 
      imgSwitcher.setImageResource(imgs2[position]); 
      drawableId.setText(imgs1[position]+""); 
      Toast.makeText(MainActivity.this, imgSwitcher + "", Toast.LENGTH_LONG).show(); 
      imgSwitcher.setVisibility(View.VISIBLE); 
     } 
    }); 
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View view) { 
      postPicture(); 
     } 

     public boolean postPicture() { 
      String a = drawableId.getText().toString(); 
      stringCopy.setText(a); 

      Bitmap b = BitmapFactory.decodeFile(a); 
      Intent share = new Intent(Intent.ACTION_SEND); 
      share.setType("image/*"); 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      b.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
      File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"); 
      try { 
       f.createNewFile(); 
       FileOutputStream fo = new FileOutputStream(f); 
       fo.write(bytes.toByteArray()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg")); 
      startActivity(Intent.createChooser(share, "Share image File")); 

      Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_LONG).show(); 
      return true; 
     } 

     }); 



    } 

そして、ここで私のエラーです:

E/BitmapFactory:java.io.FileNotFoundException::R.drawable.pg1:ストリームをデコードすることができませんオープンに失敗しました:ENOENT(そのようなファイルまたはディレクトリ) D/AndroidRuntime:VMをシャットダウンする E/AndroidRuntime:致命的な例外:メイン プロセス:com.example.mt_br.floatingactionbutton、PID:13647 java.lang.NullPointerException:仮想メソッド 'boolean android.graphics.Bitmapを呼び出そうとしています。ヌルオブジェクト参照で圧縮(android.graphics.Bitmap $ CompressFormat、int、java.io.OutputStream) 'com.example.mt_br.floatingactionbutton.MainActivity $ 2.onClick(MainActivity.java:101)

答えて

0

でcom.example.mt_br.floatingactionbutton.MainActivity $ 2.postPicture(MainActivity.java:112)で 私はこれを置換:

 public boolean postPicture() { 

      int Position = gallery.getSelectedItemPosition(); 
      Bitmap b = BitmapFactory.decodeResource(getResources(), imgs2[Position]); 
関連する問題