0

This fist method is working fine but I am not able to save this image file to a cache. Like I want to save it temporarily.

btnShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BitmapDrawable drawable = (BitmapDrawable) ivMeme.getDrawable();
                Bitmap b = drawable.getBitmap();
                //Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.refer_pic);
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/*");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                String path = MediaStore.Images.Media.insertImage(getContentResolver(),
                        b, "MEME", "Just A MEME");
                Uri imageUri = Uri.parse(path);
                share.putExtra(Intent.EXTRA_STREAM, imageUri);
                share.putExtra(Intent.EXTRA_TEXT, "Shared via MemeSharing App");
                startActivity(Intent.createChooser(share, "Share this Meme using"));
        }
        });

I am not able to convert this method to save file at getExternalCacheDir()

This second Method Just able to save file locally but not able to share that image file. As goes for share app goes down.

btnShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
BitmapDrawable drawable = (BitmapDrawable) ivMeme.getDrawable();
                Bitmap bitmap = drawable.getBitmap();

                File filepath= Environment.getExternalStorageDirectory();
                File dir = new File(filepath.getAbsolutePath()+"/Demo/");
                Log.d("Error:01",dir.getAbsolutePath());
                dir.mkdir();
                File file = new File(dir,System.currentTimeMillis()+".jpg");

                try {
                    Toast.makeText(MainActivity.this,"InSide Try ",Toast.LENGTH_SHORT).show();
                    outputStream = new FileOutputStream(file);
                } catch (FileNotFoundException e) {
                    Log.d("Error:1",e.getMessage());
                    e.printStackTrace();

                }
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                Toast.makeText(MainActivity.this,"Saved Successfully "+file.getAbsolutePath(),Toast.LENGTH_LONG).show();
                try {
                    outputStream.flush();
                } catch (IOException e) {
                    Log.d("Error:2",e.getMessage());
                    e.printStackTrace();
                }
                try {
                    outputStream.close();
                } catch (IOException e) {
                    Log.d("Error:3",e.getMessage());
                    e.printStackTrace();
                }

                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/*");
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                startActivity(Intent.createChooser(share,"Share via"));
          }
        });

I don't know why second method is not working? I have tried both method for sharing Image of ImageView, only one of them is working.

Suthar
  • 91
  • 2
  • 8
  • 1
    Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Aug 02 '21 at 10:55
  • As it's get closed if I try to save file otherwise it run perfectly. So I think this is different then mentioned one. – Suthar Aug 02 '21 at 11:06
  • `dir.mkdirs();` Bad coding. Better: `if(!dir.exists()) if(!dir.mkdirs()) return;` Also display a Toast() to inform the user. Please adapt your code and try again. – blackapps Aug 02 '21 at 11:15
  • It is not showing any Toast, which I have added for `dir.mkdirs()` – Suthar Aug 02 '21 at 11:29
  • So the conclusion was....? And you forget to return and stop. – blackapps Aug 02 '21 at 11:50
  • `catch (Exception e){ e.printStackTrace(); }` if there is a catch you should display a Toast to inform the user. And return/stop. Now you continue as if all went ok. – blackapps Aug 02 '21 at 11:55
  • Atleast one them should popup...But this is not happening. `if(!dir.exists()) { if(dir.mkdirs()) Toast.makeText(MainActivity.this, "Directory Created", Toast.LENGTH_SHORT).show(); else Toast.makeText(MainActivity.this, "Not able to create directory", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "Directory already exists", Toast.LENGTH_SHORT).show(); }` Either Directory exists or not any condition one of these Toast should popup.. – Suthar Aug 02 '21 at 21:35

0 Answers0