用户应该能够在显示图像时将该图像发送到任何社交网络

时间:2018-05-08 04:04:03

标签: android android-studio

我有一个drawable图像列表(android studio) 我用圆圈指示器呈现它们, 所以我想要在显示图像的时候。

我可以将该图像发送到任何社交网络,whatsapp,facebook等... 但我不知道我做错了什么。

以下是我的代码段:

     Intent sendIntent = new Intent(Intent.ACTION_SEND);

        viewPager= findViewById(R.id.viewPager_id);
        circleIndicator= findViewById(R.id.circleindicator_id);
        adapter = new ImageAdapter(getApplicationContext());
        viewPager.setAdapter(adapter);
        circleIndicator.setViewPager(viewPager);
    }

    public void ejecutar_info(View view){
        Intent i = new Intent(this, Info.class);
        startActivity(i);
    }
    public void pasar_imagenes (View view){

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setAction(Intent.EXTRA_STREAM);
        intent.setType("image/*");
        startActivity(Intent.createChooser(intent, "Enviar imagen a:.."));
    }

    public void salir(View view){
        finish();
    }
    @Override public boolean onCreateOptionsMenu (Menu mimenu){
        getMenuInflater().inflate(R.menu.menu_en_activity, mimenu);
        return true;
    }

    @Override public boolean onOptionsItemSelected(MenuItem opcion_menu){
        int id=opcion_menu.getItemId();

        if (id==R.id.informacion){
            ejecutar_info(null);
        }

        if (id==R.id.compartir) {
            pasar_imagenes(null);
        }

        if (id==R.id.salir){
            salir(null);
        }
        return super.onOptionsItemSelected(opcion_menu); 
    }

}

1 个答案:

答案 0 :(得分:-1)

这是了解流程的代码:

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText et_text;
    ImageView iv_image;
    TextView tv_share,tv_text;
    RelativeLayout rl_main;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

    }

    private void init(){
        et_text = (EditText)findViewById(R.id.et_text);
        iv_image = (ImageView)findViewById(R.id.iv_image);
        tv_share = (TextView)findViewById(R.id.tv_share);
        rl_main = (RelativeLayout)findViewById(R.id.rl_main);
        tv_text= (TextView) findViewById(R.id.tv_text);

        File dir = new File("/sdcard/Testing/");
        try {
            if (dir.mkdir()) {
                System.out.println("Directory created");
            } else {
                System.out.println("Directory is not created");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        tv_share.setOnClickListener(this);

        et_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_text.setText(et_text.getText().toString());

            }
        });


    }




    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.tv_share:
                Bitmap bitmap1 = loadBitmapFromView(rl_main, rl_main.getWidth(), rl_main.getHeight());
                saveBitmap(bitmap1);
                String str_screenshot = "/sdcard/Testing/"+"testing" + ".jpg";

                fn_share(str_screenshot);
                break;
        }

    }

    public void saveBitmap(Bitmap bitmap) {
        File imagePath = new File("/sdcard/Testing/"+"testing" + ".jpg");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();

            Log.e("ImageSave", "Saveimage");
        } catch (FileNotFoundException e) {
            Log.e("GREC", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }
    }

    public static Bitmap loadBitmapFromView(View v, int width, int height) {
        Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);

        return b;
    }

    public void fn_share(String path) {

        File file = new File("/mnt/" + path);

        Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
        Uri uri = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);

        startActivity(Intent.createChooser(intent, "Share Image"));


    }
}