java.lang.IllegalStateException:通过Intent.createChooser传递位图时

时间:2018-08-29 17:08:43

标签: android android-bitmap android-intent-chooser

此活动将图像添加签名和QR码到该图像以及名为mutableBitmap的drawBitmap。我必须通过所有可用的应用程序共享它

在单击按钮时添加共享方法后发生错误。如果我评论共享部分

错误

java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
    at android.view.View.performClick(View.java:5215)
    at android.view.View$PerformClick.run(View.java:21196)
    at android.os.Handler.handleCallback(Handler.java:742)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5613)
    at java.lang.reflect.Method.invoke(Native Method)

Certificate.java

public class Certificate extends AppCompatActivity {

    private ImageView img1,qrcode;
    public Bitmap mutableBitmap;


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

        img1=(ImageView) findViewById(R.id.img1);

        // Getting name saved on Shared Preferance for Certificate

        SharedPreferences prefs = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
        String name = prefs.getString("name","");


        int certificate_height,certificate_width;   // used to position name over signature


        String text="Ravi Ranjan" ; // Whatever you need to encode in the QR code
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        try {
            //QR CODE DEPENDENCY CODE
            BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE,200,200);
            BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
            Bitmap qrcode_bitmap = barcodeEncoder.createBitmap(bitMatrix);


            //Drawing Signature on IMAGE
            Bitmap certificate_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.certificate);// Load your bitmap here
            certificate_height=certificate_bitmap.getHeight();
            certificate_width=certificate_bitmap.getWidth();
            mutableBitmap = certificate_bitmap.copy(Bitmap.Config.ARGB_8888, true);
            Canvas canvas = new Canvas(mutableBitmap);
            Paint paint = new Paint();
            paint.setColor(Color.BLACK);
            paint.setTextSize(50);
            canvas.drawText(name.toString(), certificate_width-600, certificate_height-350, paint); // Drawing Signature
            canvas.drawBitmap(qrcode_bitmap,250,1100,paint); // Drawing QR CODE
            img1.setImageBitmap(mutableBitmap);
} catch (WriterException e ) {
            e.printStackTrace();
        }
    }
    //method to Share bitmap

    public void share(View view){
        String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), mutableBitmap,"certificate", null);
        Uri bitmapUri = Uri.parse(bitmapPath);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/png");
        intent.putExtra(Intent.EXTRA_STREAM, bitmapUri );
        startActivity(Intent.createChooser(intent, "Share"));
    }
}

activity_certificate.xml

<Button
        android:id="@+id/but_share"
        android:layout_width="match_parent"
        android:onClick="share"
        android:layout_height="50dp"
        android:text="Share" />

0 个答案:

没有答案