Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中共享imageSwitcher中的图像_Android_Share_Gallery_Bitmapfactory_Imageswitcher - Fatal编程技术网

如何在android中共享imageSwitcher中的图像

如何在android中共享imageSwitcher中的图像,android,share,gallery,bitmapfactory,imageswitcher,Android,Share,Gallery,Bitmapfactory,Imageswitcher,如何编写代码以共享gallery中查看的当前图像(在imageSwitcher中)。我有一个应用程序,它在一个图库中有多个图像,选中后将加载到imageSwitcher中。所以现在我想使用Share Intent弹出的Share Options窗口来共享当前正在查看和发送的图像 这是我的代码: TextView-drawableId;文本视图字符串复制 int imgs[] = { R.drawable.pg1,

如何编写代码以共享gallery中查看的当前图像(在imageSwitcher中)。我有一个应用程序,它在一个图库中有多个图像,选中后将加载到imageSwitcher中。所以现在我想使用Share Intent弹出的Share Options窗口来共享当前正在查看和发送的图像

这是我的代码: TextView-drawableId;文本视图字符串复制

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

       });



    } 
int-imgs[]=
{
R.drawable.pg1,
R.drawable.pg2,
};
字符串imgs1[]=
{
“R.drawable.pg1”,
“R.drawable.pg2”,
};
int imgs2[]=
{
R.drawable.infinity_96_58uvb_pg1,
R.drawable.infinity_96_58uvb_pg2,
};
图像切换器IMG开关,图像切换器2;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.activity_main);
drawableId=(TextView)findViewById(R.id.TextView);
stringCopy=(TextView)findViewById(R.id.textView2);
imgSwitcher=(图像切换器)findViewById(图像切换器1);
imgSwitcher.setFactory(本);
最终画廊画廊=(画廊)findViewById(R.id.gallery1);
gallery.setAdapter(新的MainActivity.ImageAdapter(this));
gallery.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
公共控件单击(AdapterView父对象、视图v、内部位置、,
长id){
imgSwitcher.setImageResource(imgs2[位置]);
drawableId.setText(imgs1[位置]+“”);
Toast.makeText(MainActivity.this,imgSwitcher+“”,Toast.LENGTH_LONG).show();
imgSwitcher.setVisibility(View.VISIBLE);
}
});
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
后期();
}
公共布尔后置图像(){
字符串a=drawableId.getText().toString();
stringCopy.setText(a);
位图b=位图工厂.decodeFile(a);
意向共享=新意向(意向.行动\发送);
share.setType(“image/*”);
ByteArrayOutputStream字节=新建ByteArrayOutputStream();
b、 压缩(位图.CompressFormat.JPEG,100,字节);
文件f=新文件(Environment.getExternalStorageDirectory()+File.separator+“temporary_File.jpg”);
试一试{
f、 createNewFile();
FileOutputStream fo=新的FileOutputStream(f);
fo.write(bytes.toByteArray());
}捕获(IOE异常){
e、 printStackTrace();
}
share.putExtra(Intent.EXTRA_流,Uri.parse(“file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(共享,“共享图像文件”);
Toast.makeText(MainActivity.this,“单击”,Toast.LENGTH_LONG.show();
返回true;
}
});
} 
这是我的错误:

E/BitmapFactory:无法解码流:java.io.FileNotFoundException:R.drawable.pg1:打开失败:eNot(没有这样的文件或目录) D/AndroidRuntime:关闭虚拟机 E/AndroidRuntime:致命异常:主 进程:com.example.mt_br.floatingactionbutton,PID:13647 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat,int,java.io.OutputStream)” 位于com.example.mt_br.floatingactionbutton.MainActivity$2.postPicture(MainActivity.java:112) 在com.example.mt_br.floatingactionbutton.MainActivity$2.onClick(MainActivity.java:101)

中,我替换了以下内容:

     public boolean postPicture() {

            int Position = gallery.getSelectedItemPosition();
            Bitmap b = BitmapFactory.decodeResource(getResources(), imgs2[Position]);