2015-12-14 54 views
1

我有一個gridview不同的圖像。當我點擊特定的imageButton時,它會下載並保存到我的SDcard文件夾中。完成後下載此圖像從下載的文件夾獲取並打開另一個活動,但我想替換特定的網格視圖單元格。如何在android中的gridview中替換單個圖像?

我做了以下任務:

  1. 圖像保存到SD卡的文件夾。
  2. 從SDcard文件夾讀取圖像。
  3. 在另一個活動中打開。

但是如何替換特定的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具有imageview的和其中的ImageButton上的GridView的每個小區設置。

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

我該如何解決了,請幫幫我!

+1

用下載圖像的標識標記'gridview單元格'。下載後通過'Tag'獲取'gridview cell'並將圖像替換爲'drawable' –

回答

1

好吧我會試着給你一個快速的解決方案,但如果你想了解MVC,你可以將數據表示爲Java對象,那將會很好。基於這些對象的狀態,您可以修改視圖。這個想法將是一個String字段,用於存儲目錄(sdcard/img)和int字段(R.id.img)中img的路徑。根據你的字符串字段的值,你可以使用它,或者從你的int字段中使用它。例如對於例如

public class Img{ 
    String name; 
    String imgSdCardPath; 
    int imgResourceValue; 
} 

然後,您的適配器將對這些Img對象的列表進行操作。

而不是String[] name;int[] imageId;列表在您的適配器,你會有。

List<Img> imageList; 

現在你需要一個邏輯設置的基礎上String imgSdCardPath;在getView()方法的value的圖像源。如果imgSdCardPathIsPresent使用下面的代碼從SD卡獲取文件

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options); 

否則做你正在做的事情。

記住在將圖像存儲到SD卡時更新您的對象中的imgSdCardPath,並調用適配器的notififydatasetchanged。

0

你爲什麼不嘗試imageloader.jar? 您所做的一切和您想要做的一切只能由一條語句來替代。 ImageLoader.getInstance().displayImage(imgUrl, holder.img);

0

嘗試看看通用圖像裝載機這裏:the github url

特點:

  • 多線程的圖像加載(異步或同步)
  • ImageLoader的的配置的寬定製(線程執行者,下載器,解碼器,內存和磁盤緩存,顯示圖像選項等)
  • 許多針對每個顯示圖像調用的自定義選項(存根圖像,緩存切換,解碼選項,Bi TMAP處理和顯示等)在存儲器和/或磁盤上(設備的文件系統或SD卡)
  • 聽力加載過程 圖像緩存(包括下載進度)

希望能幫助你!

相關問題