0

Get multiple images which are under drawable folder and show it in grid view.

After presenting it in the grid view,when we click a particular image ,it must be stored in device internal memory. the code what I implemented is shown below

public class Example3 extends AppCompatActivity {
    String[] web = {"image1", "image2", "image3",
            "image4", "image5", "image6", "image7", 
            "image8", "image9", "image10", "image11"};

    Integer[] displayImages = {R.drawable.image1, R.drawable.image2, R.drawable.image3, 
            R.drawable.image4, R.drawable.image5, R.drawable.image6, 
            R.drawable.image7, R.drawable.image8, R.drawable.image9, R.drawable.image10, R.drawable.image11};
    GridView gridView;

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

        gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(new ImageAdapter(this));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Toast.makeText(Example3.this, "You Clicked at " + web[+position] + " saved Successfully", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private class ImageAdapter extends BaseAdapter {

        Context context;

        public ImageAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return displayImages.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            ImageView imageView = new ImageView(this.context);
            imageView.setImageResource(displayImages[position]);
            imageView.setLayoutParams(new GridView.LayoutParams(180, 150));
            return imageView;
        }
    }

}

My question is - how to store a particular image in the internal memory?

DimaSan
  • 12,264
  • 11
  • 65
  • 75
raj
  • 148
  • 13

3 Answers3

0
@Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            ImageView imageView = new ImageView(this.context);
            imageView.setImageResource(displayImages[position]);
            imageView.setLayoutParams(new GridView.LayoutParams(180, 150));
            //add an onClickListener for the image view here to identify the specific imageview
            return imageView;
        }
Nakul Sudhakar
  • 1,574
  • 1
  • 24
  • 24
0

Say you have an image namely ic_launcher in your drawable. Then get a bitmap object from this image like:

Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher);

The path to SD Card can be retrieved using:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

Then save to sdcard on grid click using:

File file = new File(extStorageDirectory, "ic_launcher.PNG");
    outStream = new FileOutputStream(file);
    bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
    outStream.flush();
    outStream.close();

Don't forget to add android.permission.WRITE_EXTERNAL_STORAGE permission.

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
0

call this method in your image click listener:

 public void addImageIntoSdcard(int position) {

    //String extStorageDirectory = Environment.getExternalStorageDirectory().toString()+"/MyImages";
    File direct = new File(Environment.getExternalStorageDirectory(),"DEMO");

    if (!direct.exists()) {
        direct.mkdirs(); //directory is created;

    }

    Bitmap bm = BitmapFactory.decodeResource( getResources(), displayImages[position]);


    File file = new File(direct, web[position] + ".jpg");
    if(file.exists()){
        Toast.makeText(Example3.this, "You Clicked at " + web [+ position] + " already exists", Toast.LENGTH_SHORT).show();

    }
    else {
        Toast.makeText(Example3.this, "You Clicked at " + web [+ position] + " saved Successfully", Toast.LENGTH_SHORT).show();

    }

    FileOutputStream outStream = null;
    try {
        outStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
    try {
        outStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        outStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

Make sure you have to set permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
raj
  • 148
  • 13
vishal patel
  • 294
  • 2
  • 4
  • 13