2013-11-23 3 views
0

에서의 GridView에서 바이트 배열을 설정 나는 체크 박스의 온 클릭 리스너가 여기에 설정이 있습니다는 의도

@Override 
       public void onClick(View v) { 
        if (addCheckbox.isChecked()) { 
         System.out.println("Checked"); 
         PackageManager pm = mContext.getPackageManager(); 
         Drawable icon = null; 
         try { 
          icon = pm 
          .getApplicationIcon(entry.activityInfo.packageName); 
         } catch (NameNotFoundException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         Drawable default_icon = pm.getDefaultActivityIcon(); 
         if (icon instanceof BitmapDrawable 
           && default_icon instanceof BitmapDrawable) { 
          BitmapDrawable icon_bd = (BitmapDrawable) icon; 
          Bitmap icon_b = icon_bd.getBitmap(); 
          BitmapDrawable default_bd = (BitmapDrawable) pm 
            .getDefaultActivityIcon(); 
          Bitmap default_b = default_bd.getBitmap(); 
          if (icon_b == default_b) { 
           // It's the default icon 
           scaleDownBitmap(default_b, 100, v.getContext()); 
           Log.d("AppInfoAdapter", "Scale Bitmap Chosen"); 

           ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
           default_b.compress(Bitmap.CompressFormat.PNG, 100, stream); 
           byte[] byteArray = stream.toByteArray(); 
           Log.d("AppInfoAdapter", "Scale Bitmap to Array"); 

           Intent intent = new Intent(v.getContext(), Drag_and_Drop_App.class); 
           intent.putExtra("picture", byteArray); 
           v.getContext().startActivity(intent); 
           Log.d("AppInfoAdapter", "Intent started to send Bitmap"); 
          } 
         } 
        } else { 
         System.out.println("Un-Checked"); 
        } 

       } 
      }); 

을 여기에 내 GRIDVIEW 여기 전송 텐트 (즉, 비트 맵을 포함) 얻기 위해 노력하고 있어요 (이것은 GRIDVIEW 어댑터)입니다 :

// Keep all Images in array list 
public ArrayList<Integer> drawables = new ArrayList<Integer>(); 

// Constructor 
public GridViewAdapter(Context c){ 
    mContextGV = c; 
    Log.d("GridViewAdapter", "Constructor is set"); 

    drawables.add(R.drawable.pattern1); 
    Log.d("GridViewAdapter", "pattern1 added"); 

    drawables.add(R.drawable.pattern2); 
    Log.d("GridViewAdapter", "pattern2 added"); 

    drawables.add(R.drawable.trashcan); 
    Log.d("GridViewAdapter", "trashcan added"); 

    drawables.add(R.drawable.ic_launcher); 
    Log.d("GridViewAdapter", "ic_launcher added"); 
} 

하지만 내 어댑터를 얻기 위해 아무것도하지 않기 때문에, 나는 여기에 비트 맵을 얻을 것이다 : (내 GRIDVIEW 실제로 설정되는 경우) :

// set layout for the main screen 
    setContentView(R.layout.drag_and_drop_app); 

    // GridView 
    Log.d("D&D", "onCreate called"); 

    Bundle extras = getIntent().getExtras(); 
    byte[] byteArray = extras.getByteArray("picture"); 
    Bitmap default_b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 

    android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.GRIDVIEW1); 

    // Instance of Adapter Class 
    gridView.setAdapter(new GridViewAdapter(this)); 

하지만 그 비트 맵 default_b를 gridView에 추가 할 수 없습니다.

어떻게하면됩니까?

업데이트] 코딩을 :

public void SaveImage(Bitmap default_b) { 

    String root = Environment.getExternalStorageDirectory().toString(); 
    File myDir = new File(root + "/saved_images"); 
    myDir.mkdirs(); 
    Random generator = new Random(); 
    int n = 100000; 
    n = generator.nextInt(n); 
    String fname = "Image-" + n +".jpg"; 
    File file = new File (myDir, fname); 
    if (file.exists()) file.delete(); 
    try { 
     FileOutputStream out = new FileOutputStream(file); 
     default_b.compress(Bitmap.CompressFormat.JPEG, 90, out); 
     out.flush(); 
     out.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

답변

0

당신은 의도로 바이트 배열을 전달해서는 안 : 여기

    @Override 
       public void onClick(View v) { 
        if (addCheckbox.isChecked()) { 
         System.out.println("Checked"); 
         PackageManager pm = mContext.getPackageManager(); 
         Drawable icon = null; 
         try { 
          icon = pm 
          .getApplicationIcon(entry.activityInfo.packageName); 
         } catch (NameNotFoundException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         Drawable default_icon = pm.getDefaultActivityIcon(); 
         if (icon instanceof BitmapDrawable 
           && default_icon instanceof BitmapDrawable) { 
          BitmapDrawable icon_bd = (BitmapDrawable) icon; 
          Bitmap icon_b = icon_bd.getBitmap(); 
          BitmapDrawable default_bd = (BitmapDrawable) pm 
            .getDefaultActivityIcon(); 
          Bitmap default_b = default_bd.getBitmap(); 
          if (icon_b == default_b) { 
           // It's the default icon 
           scaleDownBitmap(default_b, 100, v.getContext()); 
           Log.d("AppInfoAdapter", "Scale Bitmap Chosen"); 

           SaveImage(default_b); 

           Intent intent = new Intent(v.getContext(),Drag_and_Drop_App.class); 
           intent.putExtra("picture", fname); 
           v.getContext().startActivity(intent); 
           Log.d("AppInfoAdapter", "Intent started to send Bitmap"); 

          } 
         } 
        } else { 
         System.out.println("Un-Checked"); 
        } 

       } 
      }); 

    // return view 
    return v; 
} 

와는 클래스입니다. 인 텐트 번들은 액티비티간에 전송 될 때 최대 크기를 가지며, 또한 직렬화 및 비 직렬화 될 때 애플리케이션을 다운시킬 수 있습니다.

정확한 문제를 설명하는 this issue도 참조하십시오. 문제가 "의도 한대로 작동 함"으로 종료되었습니다. Romain Guy의 말 :

많은 양의 데이터를 인 텐트를 통해 전달하는 것은 비용이 많이 듭니다. 비트 맵 전송 메커니즘으로 인 텐트를 사용해서는 안됩니다. 대신, URI를 또는 다른 위치 메커니즘 (파일 경로 등)

내가 캐시 디렉토리에 이미지를 작성하고 다른 활동에서 비동기 적으로 읽어을 촉구 주위에 전달합니다.

+0

캐시 디렉토리에 이미지를 쓰려면 어떻게해야합니까? – user2909006

+0

또한 어떻게 gridView에서 캐시 디렉토리를 읽을 수 있으므로 gridView에서 사용할 수 있습니까? 캐시 디렉토리를 살펴본 후 URL이있는 인터넷의 이미지에만있는 것처럼 보입니다. 내가 틀리거나 잘못했다면 나는 이것에 대해 새 것으로 알려 주시기 바랍니다. – user2909006

+0

URI에서 이미지를 읽을 수있는 메커니즘이 이미있는 경우에는 로컬 URI (캐시 된 이미지의 절대 경로 다음에 오는 'file : //')를 사용하여로드 할 수 있습니다. 이미지 작성과 관련하여, 예를 들어 [이 대답을보십시오] (http://stackoverflow.com/a/673014/154306). –