Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

This is the code to save the image from Imageview to galley in Android Studio 📸

Notifications You must be signed in to change notification settings

Zeyu-Li/SaveImagetoGallery

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>'

activity_main.xml

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageview"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/_35sdp"
        />

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save Image"
        android:id="@+id/savegallery"
        />

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ImageView imageView;

    Button saveimage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView)findViewById(R.id.imageview);

        saveimage = (Button)findViewById(R.id.savegallery);

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);


        saveimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveToGallery();
            }
        });

      
    }


    private void saveToGallery(){
            BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
            Bitmap bitmap = bitmapDrawable.getBitmap();

        FileOutputStream outputStream = null;
        File file = Environment.getExternalStorageDirectory();
        File dir = new File(file.getAbsolutePath() + "/MyPics");
        dir.mkdirs();

        String filename = String.format("%d.png",System.currentTimeMillis());
        File outFile = new File(dir,filename);
        try{
            outputStream = new FileOutputStream(outFile);
        }catch (Exception e){
            e.printStackTrace();
        }
        bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
        try{
            outputStream.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            outputStream.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

About

This is the code to save the image from Imageview to galley in Android Studio 📸

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published