2016-11-04 145 views
-2

我有一組我在imageview的time.What的指定的時間間隔,我想,當我點擊圖片我要顯示在另一個activity.How該圖像期間顯示陣列的我能做到這一點如何將圖像發送到android中的另一個活動?

代碼: -

int[] imageArray = {R.drawable.amazon, R.drawable.app_me, 
     R.drawable.borabora, R.drawable.dubai}; 
public void showImage(){ 
    m_oHandler = new Handler(); 
    Runnable oRunnable = new Runnable() { 
     int i = 0; 

     @Override 
     public void run() { 
      img.setImageResource(imageArray[i]); 
      i++; 
      if (i > imageArray.length - 1) { 
       i = 0; 
      } 
      img.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // Didn't know where to go 
        Intent intent = new Intent(MainActivity.this, Second.class); 
        intent.putExtra("BitmapImage", imageArray); 
       } 
      }); 
      m_oHandler.postDelayed(this, 6000); 

     } 
    }; 
    m_oHandler.postDelayed(oRunnable, 6000); 
} 
+0

你也可以傳遞數組列表的位置,使ArrayList的靜態的,所以你可以在全球使用它Runnable可能導致內存泄漏。 –

+0

如何................. – Raghav

+0

定義res/values/array.xml文件並僅將位置傳遞給下一個活動。在下一個活動中訪問數組並獲取圖像:) –

回答

1

不傳遞位圖對象。
而不是傳遞可繪製資源的id。

Intent intent = new Intent(MainActivity.this, Second.class); 
intent.putExtra("image_res_ids", CollectonUtils.join("/", imageArray); 

而且在Second活動,

getIntent().getExtra("image_res_ids"); 

獲取圖像資源ID陣列。

0

你的圖像是可繪製的資源,它們只不過是一個int。所以你可以把它們作爲額外的意圖傳遞給Intent。

+0

................................ .... – Raghav

0

您可以將陣列的形式發送額外的,然後用getIntent().getIntArrayExtra(EXTRA_IMAGE)

去取有關詳細信息看看Intents

1

希望當我點擊圖片我要顯示在另一個活動的形象。

使用imgsetTag()方法保存當前繪製ID和拿回來就來看點擊使用getTag()

設置繪製ID使用setTag:

.... 
img.setImageResource(imageArray[i]); 
img.setTag(imageArray[i]); 
.... 

2.在onClick方法中從v獲取ID:

int click_drawable_id=Integer.parseInt(v.getTag().toString()); 
intent.putExtra("clicked_img_draw_id", click_drawable_id); 

現在不是通過imageArray數組,只需使用clicked_img_draw_id即可獲取可繪製的ID並將其傳遞給setImageResource以在另一個活動中顯示點擊的圖像。

+0

以及如何獲得額外 – Raghav

+1

@Raghav:使用'INT draw_id = getIntent()getIntExtra( 「clicked_img_draw_id」,-1)。在第二個活動的創造中 –

0

您使用setTag保存索引/資源id,即如下執行img.setTag(index)。 而fowarding它,你可以從標籤刪除索引/資源ID,並通過作爲意圖額外

@Override 
     public void run() { 
      img.setImageResource(imageArray[i]); 
      img.setTag(i) 
      i++; 
      //your code 
      img.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // Didn't know where to go 
        Intent intent = new Intent(MainActivity.this, Second.class); 
        intent.putExtra("IMAGE_RESOURCE", img.getTag()); 
       } 
      }); 
0

只是嘗試發送您所選擇的圖像的位置

Intent i = new Intent(getApplicationContext(), FullImageActivity.class); 
       // passing array index 
       i.putExtra("id", position); 
       startActivity(i); 

獲得意圖

Intent i = getIntent(); 

     // Selected image id 
     int position = i.getExtras().getInt("id"); 
     ImageAdapter imageAdapter = new ImageAdapter(this); 

     ImageView imageView = (ImageView) findViewById(R.id.full_image_view); 
     imageView.setImageResource(imageAdapter.mThumbIds[position]); 

檢查這更多http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/

0
If you want to pass single image to next activity, than in your case, only pass image index like 

Intent intent = new Intent(MainActivity.this, Second.class); 
        intent.putExtra("BitmapImage", imageArray[i]); 

and on next activity get it. like 
int image_id= getIntent().getIntExtra("BitmapImage",0); 

this image_id is your selected image resouce id and you can set this as setImageresouce to image view. 
0
previewView = (ImageView) findViewById(R.id.imgPostIssue); 

第一個活動

Intent intent = new Intent(AddNewIssue.this, PostNewIssue.class); 
     intent.putExtra("picture", byteArray); 
     finish(); 
     startActivity(intent); 

次活動

Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      byte[] byteArray = extras.getByteArray("picture"); 
      if (byteArray != null) { 
       Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
       ImageView image = (ImageView) findViewById(R.id.imgPostIssue); 

       image.setImageBitmap(bmp); 
      } 
     } 

嘗試了這一點,它肯定會工作..希望這會幫助你。

0

嘗試使用setTag()。檢查了這一點

BitmapCache.java

public class BitmapCache { 

    private static SparseArray<Bitmap> _bitmapCache = new SparseArray<>(); 

    public static void fillBitmapCache(@NonNull Resources resources) { 
     if (null == _bitmapCache) 
      _bitmapCache = new SparseArray<>(); 

     if (_bitmapCache.indexOfKey(R.drawable.amazon) < 0) 
      _bitmapCache.append(R.drawable.amazon, BitmapFactory.decodeResource(resources, R.drawable.amazon)); 

     if (_bitmapCache.indexOfKey(R.drawable.app_me) < 0) 
      _bitmapCache.put(R.drawable.app_me, BitmapFactory.decodeResource(resources, R.drawable.app_me)); 

     if (_bitmapCache.indexOfKey(R.drawable.borabora) < 0) 
      _bitmapCache.put(R.drawable.borabora, BitmapFactory.decodeResource(resources, R.drawable.borabora)); 

     if (_bitmapCache.indexOfKey(R.drawable.dubai) < 0) 
      _bitmapCache.put(R.drawable.dubai, BitmapFactory.decodeResource(resources, R.drawable.dubai)); 
    } 

    public static Bitmap getAt(int position) { 
     return get(keyAt(position)); 
    } 

    public static int keyAt(int position) { 
     return _bitmapCache.keyAt(position); 
    } 

    public static Bitmap get(@DrawableRes int resId) { 
     return _bitmapCache.get(resId); 
    } 

    public static boolean has(@DrawableRes int resId) { 
     return _bitmapCache.indexOfKey(resId) < 0; 
    } 

    public static void append(@DrawableRes int resId, @NonNull Resources resources) { 
     _bitmapCache.append(resId, BitmapFactory.decodeResource(resources, resId)); 
    } 

    public static void put(@DrawableRes int resId, @NonNull Resources resources) { 
     _bitmapCache.put(resId, BitmapFactory.decodeResource(resources, resId)); 
    } 

    public static int size() { 
     return _bitmapCache.size(); 
    } 
} 

活動首先

private void showImage() { 
     BitmapCache.fillBitmapCache(getResources()); 

     m_oHandler = new Handler(); 
     Runnable oRunnable = new Runnable() { 
      int i = 0; 

      @Override 
      public void run() { 
       img.setImageBitmap(BitmapCache.getAt(i)); 
       img.setTag(BitmapCache.keyAt(i)); 
       i++; 
       if (i >= BitmapCache.size()) { 
        i = 0; 
       } 
       img.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (null != v.getTag()) { 
          int resId = (int) v.getTag(); 
          Intent intent = new Intent(MainActivity.this, Second.class); 
          intent.putExtra("BitmapImage", resId); 
         } 
        } 
       }); 
       m_oHandler.postDelayed(this, 6000); 

      } 
     }; 
     m_oHandler.postDelayed(oRunnable, 6000); 
    } 

次活動

private void displayImageOnSecond() { 
     Bundle bundle = getIntent().getExtras(); 
     int resId = bundle.getInt("BitmapImage", 0); 

     if (resId > 0) { 
      secondImage.setImageBitmap(BitmapCache.get(resId)); 
     } 
    } 

此外,我建議使用ViewPager和自定義爲自動旋轉,而不是目前使用的一種。如果Activity已被破壞

相關問題