[英]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? 我的问题是-如何在内部存储器中存储特定的图像?
@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;
}
Say you have an image namely ic_launcher in your drawable. 假设您的可绘制对象中有一个图像,即ic_launcher。 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: 可以使用以下方法检索SD卡的路径:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
Then save to sdcard on grid click using: 然后使用以下命令将其保存到网格上的sdcard中:
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. 不要忘记添加android.permission.WRITE_EXTERNAL_STORAGE权限。
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"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.