0
Hello, everyone, I’m new to the forum but whenever I can I follow some topics.
I have an Android application where it generates a bitmap and I would like to share this bitmap via Whatsapp or email but I’ve been cracking heads, if anyone can help me would be very grateful.
Follows Java code class where I put the code:
package br.com.codeapp.kcode;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ShareActionProvider;
import android.widget.Toast;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.journeyapps.barcodescanner.BarcodeEncoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main2Activity extends AppCompatActivity {
private ImageView imageView; private ShareActionProvider shareActionProvider;
EditText edtTexto; Button btnGerar; ImageView ivQRCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); // compartilhando inicio//
imageView = (ImageView) this.findViewById(R.id.ivQRCode);
// compartilhando fim//
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
inicializarComponentes();
clickButton();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.show();
}
});
}
}
private void clickButton() {
btnGerar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gerarQRCode();
}
});
}
private void gerarQRCode() {
String texto = edtTexto.getText().toString();
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitmatrix = multiFormatWriter.encode(texto ,
BarcodeFormat.QR_CODE ,2000 ,2000 ); BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitmatrix); ivQRCode.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
private void inicializarComponentes() {
edtTexto = (EditText) findViewById(R.id.editText); btnGerar = (Button) findViewById(R.id.btnGerar);
ivQRCode = (ImageView) findViewById(R.id.ivQRCode);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu)
// menu.add(0,1,0,"Share")
//.setIcon(android.R.drawable.ic_menu_share)
//.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
//@Override
//public boolean onMenuItemClick(MenuItem item) {
//share(((BitmapDrawable)ivQRCode.getDrawable()).getBitmap());
//return false;
//}
//}).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
//return true;
// compartilhamento texto parte 1 //
getMenuInflater().inflate(R.menu.menu_main,menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
mShareActionProvider = (android.support.v7.widget.ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
setShareActionIntent("teste");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
// The action bar will automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) { return true; }
return super.onOptionsItemSelected(item);
}
private android.support.v7.widget.ShareActionProvider mShareActionProvider;
// compartilhamento texto parte 2//
private void setShareActionIntent(String your_message) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,your_message);
mShareActionProvider.setShareIntent(intent);
}
// teste compartilhamento imagem//
private void compartilhar(){
if (imageView.getDrawable() != null) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
BitmapDrawable drawable = (BitmapDrawable)
imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream Bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,Bytes );
String path = MediaStore.Images.Media.insertImage(getContentResolver(),bitmap,"imagem", null);
Uri uri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(intent, "compartilhar"));
} else {
Toast.makeText(getBaseContext(),"Não possui ", Toast.LENGTH_LONG).show();
}
}
private void share(Bitmap bitmap){
// Intent shareIntent = new Intent(Intent.ACTION_SEND);
// shareIntent.setType("image/jpeg");
// try {
// File cache = getApplicationContext().getExternalCacheDir();
// File shareFile = new File(cache,"example.png");
// FileOutputStream out = new FileOutputStream(shareFile);
// bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
// out.flush();
// out.close();
// shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+shareFile));
// startActivity(Intent.createChooser(shareIntent,"Share"));
//} catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}
If possible put the generated Exception to facilitate, thank you!
– Guilherme Montanher
Thank you Guilherme, I believe this is private void gerarQRCode() { String text = edtTexto.gettext(). toString(); Multiformatwriter multiFormatWriter = new Multiformatwriter(); Try {
 BitMatrix bitmatrix = multiFormatWriter.encode(texto , 
 BarcodeFormat.QR_CODE ,2000 ,2000 ); BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
 Bitmap bitmap = barcodeEncoder.createBitmap(bitmatrix); ivQRCode.setImageBitmap(bitmap); } catch (Writerexception e) { e.printStackTrace(); } }
– Almir Kubo