2014-05-06 52 views
0

我想發送圖像到服務類的位圖,我想使用位圖有設置壁紙 我使用的代碼....如何發送位圖[]到服務類?

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) { 
      if (requestCode == PICK_IMAGE_MULTIPLE) { 
       imagesPathList = new ArrayList<String>(); 
       String[] imagesPath = data.getStringExtra("data").split("\\|"); 
       try { 
        lnrImages.removeAllViews(); 
       } catch (Throwable e) { 
        e.printStackTrace(); 
       } 
       for (int i = 0; i < imagesPath.length; i++) { 
        imagesPathList.add(imagesPath[i]); 
        yourbitmap = BitmapFactory.decodeFile(imagesPath[i]); 
        resized[i] = Bitmap.createScaledBitmap(yourbitmap, 480, 
          800, true); 
        ImageView imageView = new ImageView(this); 
        imageView.setImageBitmap(resized[i]); 
        imageView.setAdjustViewBounds(true); 
        lnrImages.addView(imageView); 
       } 
      } 
     } 

    } 



case R.id.btnsetwall: 
      Intent i = new Intent(MainActivity.this, WallService.class); 
      Log.i("Main Activity", "Before putExtra"); 
      i.putExtra("Imagess", resized); 
      Log.i("Main Activity", "After putExtra"); 
      startService(i); 
      Log.i("Main Activity", "Start Service"); 
      break; 

//如果我使用我。 putExtra(「Imagess」,調整大小);它提供了錯誤,如果我不使用該行的服務得到啓動

在服務類...

@Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     Log.i("on create", "Service Created"); 

    } 

的logcat的是...

05-06 15:53:57.932: I/Main Activity(14185): Before putExtra 
05-06 15:53:57.932: I/Main Activity(14185): After putExtra 
05-06 15:53:57.942: E/JavaBinder(14185): !!! FAILED BINDER TRANSACTION !!! 
05-06 15:53:57.952: I/Main Activity(14185): Start Service 
+0

,你初始化調整[I]? – duggu

+0

public class MainActivity extends Activity實現View.OnClickListener私有位圖resized [] = new Bitmap [10]; –

+0

resized [] = new Bitmap [imagesPath.length]; for(int i = 0; i duggu

回答

-1

,你可以在不同的不同的解決這個問題方式與您的要求。

  1. u可以使用靜態變量,用於存儲imageBitmap

  2. u能意圖通過

    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);  
    byte[] byteArray = stream.toByteArray();  
    Intent in1 = new Intent(this, Activity2.class);  
    in1.putExtra("image",byteArray); 
    
+0

我沒有得到,這就高於我的頭 –

+0

將位圖轉換爲字節[]並通過意圖傳遞 – SuN

+0

或者你可以直接將流轉換爲onActivityResult上的byteArry並用意向發送。putExtra() – SuN

0

Bitmap類本身實現parcelable接口。 所以,你可以通過調用 Bitmap[] btmaps = new Bitmap[10]; Intent intent = new Intent(); intent.putExtra("bitmaps", btmaps);

傳位圖的數組,如果你想在你的應用程序的不同領域你不通過使用位圖就可以透過這個意圖數組

Bitmap[] btmpList = (Bitmap[]) intent.getParcelableArrayExtra("bitmaps"); 
0

他們周圍。你在一個非常有限的內存機器上,儘可能少地在堆中保存一個位圖。

你這樣做的方式,你只是臨時保存到一個文件,然後加載它,無論你再次需要它。

Get your cache dir

public static String getCacheDir(Context ctx) { 
     return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||!Environment.isExternalStorageRemovable() ? 
       ctx.getExternalCacheDir().getPath() : ctx.getCacheDir().getPath(); 
    } 

保存位圖

public static File saveBitmap(Bitmap bitmap, String filename, String path, boolean recycle) { 
     FileOutputStream out=null; 
     try { 
      File f = new File(path,filename); 
      if(!f.exists()) { 
       f.createNewFile(); 
      } 
      out = new FileOutputStream(f); 
      if(bitmap.compress(Bitmap.CompressFormat.PNG, 90, out)) { 
       return f; 
      } 
     } catch (Exception e) { 
      Log.e(TAG, "Could not save bitmap", e); 
     } finally { 
      try{ 
       out.close(); 
      } catch(Throwable ignore) {} 
      if(recycle) { 
       bitmap.recycle(); 
      } 
     } 
     return null; 
    } 

然後重新裝入。

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

You could also look into Picasso負責處理異步加載你,也增強了與磁盤高速緩存性能和更多的功能

Picasso.with(getContext()).load(bitmapFile).into(imageView);