单击后如何将图像存储在内存中?

时间:2016-11-25 09:50:50

标签: android

获取多个图像,这些图像位于可绘制文件夹下,并以网格视图显示。

在网格视图中显示后,当我们单击特定图像时,它必须存储在设备内部存储器中。 我实施的代码如下所示

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;
        }
    }

}

我的问题是 - 如何将特定图像存储在内存中?

3 个答案:

答案 0 :(得分: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;
        }

答案 1 :(得分:0)

假设您的drawable中有一张图像即ic_launcher。然后从这个图像中获取一个位图对象,如:

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

可以使用以下方法检索SD卡的路径:

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

然后使用以下方法保存到网格上的sdcard:

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

不要忘记添加android.permission.WRITE_EXTERNAL_STORAGE权限。

答案 2 :(得分:0)

在图片点击监听器中调用此方法:

 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();
    }


}

确保您必须设置权限:

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