我有一个带有不同图像的gridview。当我单击特定的imageButton时,它将下载并保存到我的SDcard文件夹中。完成下载后,此图像从下载的文件夹中获取并在另一个活动中打开,但是我想在特定的gridview单元上进行替换。
我完成了以下任务:
将图像保存到SDcard文件夹。
从SDcard文件夹中读取图像。
在另一个活动中打开。
但是如何在特定的gridview单元上替换。
这是我的watercolor_fragment.xml,具有griview。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black_overlay">
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
这是我的Watercolor.java,它会增加watercolor_fragment.xml
public class Watercolor extends Fragment {
View view;
String[] name={"FIRST","SECOND","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN","ELEVEN","TWELVE","THRTEEN","FOUREEN","FIFTEEN","SIXTEEN"};
public int[] mThumbIds={R.drawable.natureone,R.drawable.naturetwo,
R.drawable.naturethree,R.drawable.naturefour,
R.drawable.naturefive,R.drawable.naturesix,
R.drawable.natureseven,R.drawable.natureight,
R.drawable.naturenine,R.drawable.natureten,R.drawable.natureleven,R.drawable.naturetwelve,
R.drawable.naturethrtineen,R.drawable.naturefourthyeen,R.drawable.naturefifteen,R.drawable.naturesixteen};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view=inflater.inflate(R.layout.watercolor_fragment, container, false);
GridView gridView=(GridView)view.findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(view.getContext(),name,mThumbIds));
// gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Intent i=new Intent(getActivity(), FullImaeActivity.class);
// Log.e("Position",""+position);
// i.putExtra("id", position);
// startActivity(i);
// }
// });
return view;
}
}
这是我的custome_image.xml,它具有在gridview的每个单元格上设置的imageview和imageButton。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:src="@drawable/naturefour"
android:layout_height="fill_parent"/>
<ImageButton
android:id="@+id/btndownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/download"
android:layout_gravity="top|right"
android:background="@android:color/transparent"/>
</FrameLayout>
这是我的ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
String[] name;
Context context;
int[] imageId;
private static LayoutInflater inflater = null;
public ImageAdapter(Context context,String[] name, int[] imageId) {
this.name=name;
this.context = context;
this.imageId = imageId;
inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.name.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder {
ImageButton btnDownload;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
final View rowView;
rowView = inflater.inflate(R.layout.custome_image, null);
holder.btnDownload = (ImageButton) rowView.findViewById(R.id.btndownload);
holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
holder.img.setAdjustViewBounds(true);
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imageId[position]);
int height = (bitmap.getHeight() * 512 / bitmap.getWidth());
Bitmap scale = Bitmap.createScaledBitmap(bitmap, 512, height, true);
holder.img.setImageBitmap(scale);
holder.btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//File root = new File(context.getFilesDir(), "IMAGES");
File root = new File(Environment.getExternalStorageDirectory().getPath() + "/.IMAGES/");
if (!root.exists()) {
root.mkdirs();
}
File file = new File(root + File.separator + position + ".jpeg");
Log.e("Path", "" + file);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent intent = new Intent(rowView.getContext(), FullImaeActivity.class);
// intent.putExtra("id", position);
try{
File root=new File(Environment.getExternalStorageDirectory().getPath() + "/.IMAGES/");
Log.e("File path",""+root);
File file=new File(root,File.separator+position+".jpeg");
if(!file.exists()){
Toast.makeText(rowView.getContext(), "Please, Download First!!!!", Toast.LENGTH_SHORT).show();
}
else{
Log.e("FINAL PATH", "" + file);
FileInputStream streamIn = new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(streamIn);
streamIn.close();
Intent intent=new Intent(rowView.getContext(),FullImaeActivity.class);
intent.putExtra("id", bitmap);
rowView.getContext().startActivity(intent);
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
return rowView;
}
}
这是我的FullImageActivity.java
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_full_imae);
mVisible = true;
mContentView = findViewById(R.id.fullscreen_content);
Intent intent=getIntent();
Bitmap bitmap=(Bitmap)intent.getParcelableExtra("id");
Watercolor adapter=new Watercolor();
imageView=(ImageView)findViewById(R.id.full_image);
//imageView.setImageResource(adapter.mThumbIds[id]);
imageView.setImageBitmap(bitmap);
}
我该如何解决,请帮帮我!!!
最佳答案
好的,我想为您提供一个快速的解决方案,但是如果您想在MVC上读起来,可以将数据表示为Java对象,那就太好了。根据这些对象的状态,您将修改视图。想法是有一个String字段(用于将img的路径存储在目录中)(sdcard / img)和一个int字段(R.id.img)。根据字符串字段的值,您可以使用它,也可以在int字段中使用它。
例如
public class Img{
String name;
String imgSdCardPath;
int imgResourceValue;
}
然后,您的适配器将在这些Img对象的列表上运行。
代替
String[] name;
和适配器中的
int[] imageId;
列表。List<Img> imageList;
现在,您需要一种逻辑来基于getView()方法中
String imgSdCardPath;
的值设置图像源。如果imgSdCardPathIsPresent使用以下代码从sdcard获取文件BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
否则做你在做什么。
将映像存储到sdcard时,请记住在对象中更新imgSdCardPath,并调用适配器的notififydatasetchanged。
关于android - 如何在android的gridview中替换单个图像?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34261036/