2017-03-26 38 views
0

我的上傳活動從圖庫中選擇圖像並上傳。我的UploadActivity和棒棒糖版本之間的衝突

我打開我的上傳活動從另一個,當我打開它在棉花糖它的工作很好,但是當我從棒棒糖打開它,這是需要太多的時間來打開。

我的代碼有什麼問題,導致這種衝突?

public class UploadActivity extends Activity { 
    // LogCat tag 
    private static final String TAG = ImagesForAds.class.getSimpleName(); 

    private int count; 
    private Bitmap[] thumbnails; 
    private boolean[] thumbnailsselection; 
    private String[] arrPath; 
    private ImageAdapter imageAdapter; 
    private static final int PICK_FROM_CAMERA = 1; 
    ArrayList<String> IPath = new ArrayList<String>(); 
    public static Uri uri; 
    final String[] columns = { MediaStore.Images.Media.DATA, 
      MediaStore.Images.Media._ID }; 
    final String orderBy = MediaStore.Images.Media._ID; 
    Cursor imagecursor = managedQuery(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, 
      null, orderBy); 
    int image_column_index = imagecursor 
      .getColumnIndex(MediaStore.Images.Media._ID); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_upload); 

     this.count = imagecursor.getCount(); 
     this.thumbnails = new Bitmap[this.count]; 
     this.arrPath = new String[this.count]; 
     this.thumbnailsselection = new boolean[this.count]; 
     for (int i = 0; i < this.count; i++) { 
      imagecursor.moveToPosition(i); 
      int id = imagecursor.getInt(image_column_index); 
      int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA); 
      thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
       getApplicationContext().getContentResolver(), id, 
       MediaStore.Images.Thumbnails.MICRO_KIND, null); 
      arrPath[i] = imagecursor.getString(dataColumnIndex); 
     } 
     GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid); 
     imageAdapter = new ImageAdapter(); 
     imagegrid.setAdapter(imageAdapter); 
     imagecursor.close(); 
     final Button uploadBtn = (Button) findViewById(R.id.uploadDONE); 
     uploadBtn.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       final int len = thumbnailsselection.length; 
       int cnt = 0; 
       String selectImages = ""; 
       for (int i = 0; i < len; i++) { 
        if (thumbnailsselection[i]) { 
         cnt++; 
         selectImages = arrPath[i]; 
         IPath.add(selectImages); 
        } 
       } 

       if (cnt == 0) { 
        Toast.makeText(getApplicationContext(), 
          "Please select at least one image", 
          Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(getApplicationContext(), 
          "You've selected Total " + cnt + " image(s).", 
          Toast.LENGTH_LONG).show(); 
        Log.d("SelectedImages", selectImages); 

        Intent intentMessage = new Intent(UploadActivity.this, 
          ImagesForAds.class); 
        intentMessage.putStringArrayListExtra("IMAGE", IPath); 
        startActivity(intentMessage); 
       } 
      } 
     }); 
    } 

    public class ImageAdapter extends BaseAdapter { 
     private LayoutInflater mInflater; 

     public ImageAdapter() { 
      mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 

     public int getCount() { 
      return count; 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 
      if (convertView == null) { 
       holder = new ViewHolder(); 
       convertView = mInflater.inflate(R.layout.galleryitem, null); 
       holder.imageview = (ImageView) convertView 
         .findViewById(R.id.thumbImage); 
       holder.checkbox = (CheckBox) convertView 
         .findViewById(R.id.itemCheckBox); 

       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 
      holder.checkbox.setId(position); 
      holder.imageview.setId(position); 
      holder.checkbox.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        CheckBox cb = (CheckBox) v; 
        int id = cb.getId(); 
        if (thumbnailsselection[id]) { 
         cb.setChecked(false); 
         thumbnailsselection[id] = false; 
        } else { 
         cb.setChecked(true); 
         thumbnailsselection[id] = true; 
        } 
       } 
      }); 
      holder.imageview.setImageBitmap(thumbnails[position]); 
      holder.checkbox.setChecked(thumbnailsselection[position]); 
      holder.id = position; 
      return convertView; 
     } 
    } 

    class ViewHolder { 
     ImageView imageview; 
     CheckBox checkbox; 
     int id; 
    } 

    @Override 
    public void onBackPressed() { 
     Intent i = new Intent(UploadActivity.this, ImagesForAds.class); 
     UploadActivity.this.finish(); 
     startActivity(i); 
     super.onBackPressed(); 
    } 
} 

回答

1

您使用大概棉花糖手機在mediastore很多更多的圖像,這就是爲什麼你的活動啓動緩慢。

這就是說,有在你的代碼一般性能比較的問題:

  • 使用方法moveToPosition在光標的每一行,更好地利用moveToFirst和使用MoveToNext方法在一個循環。

  • 您在活動開始時將所有縮略圖加載到內存中。當有許多圖像時,這可能需要很長時間,也可能會很容易用完內存。根據需要更好地將這些縮略圖加載到適配器中。

更好的是,讓你的適配器從CursorAdapter繼承,這應該給你最好的啓動時間。

+0

謝謝,但是cursorAdapter與recycleview一起工作? 以及如何我可以根據需要加載適配器中的tumbnails?請你給我看一個樣品? – user1058652