rupa sree rupa sree - 8 days ago 5
Android Question

How to store the image in the internal memory after clicking on it?

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?

Answer

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"/>