0
public class MainActivity extends Activity {

    ImageButton b1, b2;
    ImageView v1;
    private static int RESULT_LOAD_IMAGE = 1;
    BitmapDrawable drawable;
    Bitmap bitmap;

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

        b1 = findViewById(R.id.card);
        b2 = findViewById(R.id.save);
        v1 = findViewById(R.id.imageview1);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK);
                i.setType("image/*");
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                drawable = (BitmapDrawable) v1.getDrawable();
                bitmap = drawable.getBitmap();

                FileOutputStream outputStream = null;
                File sdCard = Environment.getExternalStorageDirectory();
                File directory = new File(sdCard.getAbsolutePath() + "YourFolderName");
                directory.mkdir();


                @SuppressLint("DefaultLocale") String filename = String.format("%d.jpg",System.currentTimeMillis());
                File outFile = new File(directory,filename);
                try {
                    outputStream = new FileOutputStream(outFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                    outputStream.flush();
                    outputStream.close();

                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    intent.setData(Uri.fromFile(outFile));
                    sendBroadcast(intent);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });

    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            ImageView imageView = findViewById(R.id.imageview1);
            try {
                Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
                imageView.setImageBitmap(bm);
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }
        }
    }

    public static void saveFrameLayout(FrameLayout frameLayout, String path) {
        frameLayout.setDrawingCacheEnabled(true);
        frameLayout.buildDrawingCache();
        Bitmap cache = frameLayout.getDrawingCache();
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            cache.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            frameLayout.destroyDrawingCache();
        }
    }

}

I want to save the image in the gallery. It is running but it is showing error on the run time. Here I am picking the Image from the gallery and want to save that image again in the gallery. Already given the different path to save that picked image.

It is showing

"W/System.err: at >android.app.ActivityThread.main(ActivityThread.java:6692)" while I run the >code. I already done entry in manifest file.

ZiGaelle
  • 744
  • 1
  • 9
  • 21

1 Answers1

0

Your file (or the directory to said file) doesn't exist, you'll need to create the file using createNewFile, something along the following lines should work,

File outFile = new File(directory,filename);

//The below line shouldn't be required since you do directory.mkdir()
//file.getParentFile().mkdirs(); // Will create parent directories if not exists

file.createNewFile(); // Will create file if it doesn't exist

try {
    //Rest of your code
Chrisvin Jem
  • 3,940
  • 1
  • 8
  • 24