2016-04-19 70 views
0

我想將包含圖像位置的字符串數組設置爲ImageView。字符串數組傳回活動A到活動B並在活動B中設置爲ImageView。 我應該如何設置?下面是代碼:將字符串數組設置爲ImageView

從活動一:

String[] outputStrArr = new String[selectedItems.size()]; 

        for (int i = 0; i < selectedItems.size(); i++) { 
         outputStrArr[i] = selectedItems.get(i); 
         //Log.i(GalleryActivity.TAG,outputStrArr[i]); 
        } 

        Intent intent = new Intent(getApplicationContext(), 
          MainActivity.class); 

        // Create a bundle object 
        Bundle bundle = new Bundle(); 
        bundle.putStringArray("gridItem", outputStrArr); 

        // Add the bundle to the intent. 
        intent.putExtras(bundle); 

        // start the MainActivity 
        startActivity(intent); 

從活動B:

public void onResume() { 
    super.onResume(); 

    Bundle bundle = getIntent().getExtras(); 

    if (bundle != null){ 

     String[] result = bundle.getStringArray("gridItem"); 

     if(result != null){; 
      // I want to set the String Array to ImageView here. 
     } 
    } 
} 

感謝您的幫助!

+0

定義「包含圖像」。陣列中有什麼,舉個例子。我們不知道您是否顯示 –

+0

您是否知道需要將該位置放入該陣列的imageView中? –

+0

@JuanCortes我的圖像從SD卡中加載並顯示在gridview中。我從gridview中選擇了一些圖像併發送到另一個包含imageView的活動。問題是如何在bundle.getStringArray(「gridItem」)後將圖像數組設置爲該imageView。 ??謝謝 – TZY

回答

0

首先將圖像需要包括繪製文件夾內,然後在這裏你可以做什麼:

Resources res = getResources(); 
String mDrawableName = result.get(position);; 
int resID = res.getIdentifier(mDrawableName , "drawable", getPackageName()); 
Drawable drawable = res.getDrawable(resID); 
imageView.setImageDrawable(drawable); 

這應該很好地工作。但是,如果您有其他問題,請告訴我。

編輯:

嘗試下面的代碼從存儲在SD卡中的文件設置的位圖圖像:

File imgFile = new File("/sdcard/Images/my_image.jpg"); 
if(imgFile.exists()){ 
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 
    myImage.setImageBitmap(myBitmap); 
} 

,包括此權限清單文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

但我的圖像是從SD卡加載並顯示在gridview內。當我得到多個選定的項目並存儲到字符串數組並傳遞到另一個活動(活動B)並將該多個圖像設置爲imageView。 – TZY

+0

通過記錄字符串數組的變量「結果」,我清楚地看到logcat中顯示的選定項目的數量,這意味着所選項目已成功從活動A檢索到 – TZY

+0

因此,您想要將圖像從SD卡顯示到imageView ?你知道路徑位置嗎? –