0

I'm using couple of commands from this answer to save my bitmap on SD card and then share it via intent.

and here is my final code:

                    View u = findViewById(R.id.mainL);
                    u.setDrawingCacheEnabled(true);                                                
                    LinearLayout z = (LinearLayout) findViewById(R.id.mainL);
                    int totalHeight = z.getHeight();
                    int totalWidth = z.getWidth();
                    u.layout(0, 0, totalWidth, totalHeight);    
                    u.buildDrawingCache(true);
                    Bitmap b = Bitmap.createBitmap(u.getDrawingCache()); 
                    u.setDrawingCacheEnabled(false);
                    String filePath = Environment.getExternalStorageDirectory()
                            + File.separator + "pics/screenshot.jpeg";
                    File imagePath = new File(filePath);
                    FileOutputStream fos;
                    try {
                        fos = new FileOutputStream(imagePath);
                        b.compress(CompressFormat.JPEG, 100, fos);
                        fos.flush();
                        fos.close();
                    } catch (FileNotFoundException e) {
                        Log.e("GREC", e.getMessage(), e);
                    } catch (IOException e) {
                        Log.e("GREC", e.getMessage(), e);
                    }
                    Uri bmpUri = Uri.parse(filePath);
                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                    shareIntent.setType("image/jpeg");
                    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(Intent.createChooser(shareIntent, "Share"));

but now I have two problems.

1)the result Image (screenshot.png) is not reachable with mobile gallery (there is a image file in pics folder in sd card although).

2)when I try to share it via intent, it doesn't send and for example when I send it via Bluetooth the receiver gadget breaks the sending operation.

thanks.

Community
  • 1
  • 1
SaDeGH_F
  • 471
  • 1
  • 3
  • 14

2 Answers2

1

ohk just paste this line after adding any pic in the gallery it will refresh your gallery

It worked for me hope will help u :)

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
1

try this

                //save image into sdcard
                FrameLayout f=(FrameLayout)findViewById(R.id.framelayout);                                                               
                f.setDrawingCacheEnabled(true);
                Bitmap bm = f.getDrawingCache();
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] imageInByte1 = stream.toByteArray();

                String  path = Environment.getExternalStorageDirectory().toString();
                File  imgDirectory = new File(Environment.getExternalStorageDirectory()+"/images/");
                imgDirectory.mkdirs();
                OutputStream fOut = null;
                File file = null;
                file = new File(path,"/images/"+etcardname.getText().toString()+ ".png");
                Toast.makeText(getBaseContext(), "saved at: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
                fOut = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();

              //share  image
               Intent share = new Intent(android.content.Intent.ACTION_SEND);
               share.setType("image/*");
               share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));
               startActivity(Intent.createChooser(share, "Share image using"));                                                                                    
user3355820
  • 272
  • 1
  • 8