你好,我有一個時間表應用程序,它有一個主菜單。我想有一個說'選擇圖像'或類似的按鈕,所以當用戶點擊它時,他會進入他的畫廊並選擇照片,然後節省我創建的時間表應用程序。我希望有人能幫助我,因爲我在互聯網上發現的唯一信息是我應該使用android.intent.action.PICK?在此先感謝如何選擇要用作「個人資料圖片」的圖片?
更新: 我做到了這一點,然後現在當我點擊按鈕加載畫廊(完美的作品)。一旦我選擇了一張圖片,它會說「不幸的是,時間表已經停止」。怎麼了。這是代碼。
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
//Button Sound
final MediaPlayer buttonSound = MediaPlayer.create(Main.this, R.raw.sound1);
//Setting up the button references
Button week1 = (Button) findViewById(R.id.week1button);
Button week2 = (Button) findViewById(R.id.week2button);
week1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.timetable.WEEK1"));
}
});
week2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.timetable.WEEK2"));
}
});
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private static int RESULT_LOAD_IMAGE = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
谷歌「android.intent.action.PICK」搜索它的用途以及它是否適合你。沒有?搜索「選擇圖像意圖Android」,你得到這個http://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app-in-android也許這可以幫助你。 –
你可以看看原來的帖子,因爲我改變了它。它不會似乎正常工作?!? – user3195144