2016-04-19 14 views
0

イメージ位置を含む文字列配列をImageViewに設定したいと思います。文字列配列はアクティビティAからアクティビティBに渡され、アクティビティBではImageViewに設定されます。 どのように設定する必要がありますか?以下のコードは以下のとおりです。文字列配列をImageViewに設定

アクティビティAから:

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

はSDカード内に格納されたファイルからビットマップ画像を設定するには、以下のコードを試してみてください「画像を含む」を定義します。配列の中に何が入っているか、例を挙げてください。あなたが表示されていないかどうかわからない –

+0

その配列からimageViewに配置する必要がある位置を知っていますか? –

+0

@JuanCortes私の画像はsdcardから読み込まれ、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); 

これは完璧に動作するはずです。しかし、別の問題がある場合は私に知らせてください。

EDIT:

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

しかし、私の画像はsdcardからロードされ、gridviewの中に表示されます。複数の項目を選択して文字列配列に格納し、別のアクティビティ(アクティビティB)に渡して、その複数のイメージをimageViewに設定します。 – TZY

+0

文字列array。の変数 "result"をログに記録すると、logcatに表示されている選択項目の数が表示されます。つまり、選択した項目がアクティビティAから正常に取得されたことを意味します。 – TZY

+0

SDカードからimageView ?あなたはパスの位置を知っていますか? –