2
我想出瞭如何在活動之間傳遞一個字符串值,這要感謝這個網站,但是我在傳遞圖像時遇到了問題。我想要的是有一個用戶點擊一個按鈕,打開畫廊,並允許選擇一張圖片。然後我有另一個按鈕打開另一個顯示ImageView的活動。我希望能夠將ImageView的圖像作爲以前活動中的選定圖像。將圖像傳遞給另一個活動
這裏是有我點擊打開庫和檢索所選擇的圖像按鈕類:
public class EnterEdit extends Activity implements View.OnClickListener
{
private static final int SELECT_IMAGE = 0;
String filepath;
Bundle fieldresults;
Intent b;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.enteredit);
Button selectwallpaper = (Button) findViewById(R.id.selectwallpaper);
selectwallpaper.setOnClickListener(this);
Button previewwallpaper = (Button) findViewById(R.id.previewwallpaper);
previewwallpaper.setOnClickListener(this);
fieldresults = new Bundle();
b = new Intent(this, PreviewScreen.class);
}
@Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.selectwallpaper:
Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, SELECT_IMAGE);
break;
case R.id.previewwallpaper:
startActivity(b);
}
break;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_IMAGE)
{
Uri selectedimage = data.getData();
String[] filepathcolumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedimage, filepathcolumn, null, null, null);
cursor.moveToFirst();
int columnindex = cursor.getColumnIndex(filepathcolumn[0]);
filepath = cursor.getString(columnindex);
cursor.close();
fieldresults.putString("bitmap", filepath);
b.putExtras(fieldresults);
}
}
}
}
這裏是應該顯示選擇的圖像類:
public class PreviewScreen extends Activity implements View.OnClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.previewscreen);
Bundle fieldresults = this.getIntent().getExtras();
String backgroundpath = fieldresults.getString("bitmap");
String background = BitmapFactory.decodeFile(backgroundpath);
ImageView gallerypic = (ImageView) findViewById(R.id.gallerypic);
gallerypic.setImageBitmap(background);
}
}
我不確定的是在OnActivityResult
如果我應該通過selectedImage
或b.putExtra("bitmap", selectedimage);
行中選擇的圖像。我嘗試了兩種,但是我沒有看到第二項活動的圖像。如果我正確設置了圖像視圖,我也不確定在PreviewScreen
課程中。任何幫助表示讚賞。謝謝。
所以我通過「文件路徑」變量(行 String filepath = cursor.getString(columnindex)) – joelreeves 2010-08-23 04:16:05
是的......然後,你解碼活動B中的圖像。你知道知道怎麼做:) – Cristian 2010-08-23 05:05:49
好的,我做了一些編輯,但ImageView在第二個活動中仍然是空白的。我是否正確地在第一課中傳遞它,並在第二課中正確引用它?感謝你目前的幫助。 – joelreeves 2010-08-23 05:56:41