如何让用户选择图片?

时间:2015-04-21 06:28:22

标签: android image gallery

好的,所以我对这个Android开发很新。 我有一个按钮。我想要这个按钮打开来自...有点活动的选择图像,这样用户就可以从系统或他拥有的不同图库应用程序或相机或网页中选择图像。像其他应用程序一样。我希望所选图像将获取图像ID并将其存储到数据库,并将图像显示为按钮。我会做存储,但我如何获得图像ID?和所有其他的东西?非常感谢任何帮助我的人!干杯!

添加请求代码:

public class add_requests extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_requests);
}
Globals mThumbIds = Globals.getInstance();
public void onSaveClick(View view){
    EditText desc = (EditText) findViewById(R.id.request_name);
    EditText message = (EditText) findViewById(R.id.request_message);
    String message1 = message.getText().toString();
    String desc1 = desc.getText().toString();
    Request r1 = new Request(R.drawable.iphone, message1,desc1);
    mThumbIds.getData().add(r1);
    finish();
}
public void browsePictures(){
    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);

}

}

1 个答案:

答案 0 :(得分:0)

以下是从画廊中挑选和使用图片的示例。

public void pickImage() {
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  intent.setType("image/*");
  startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
        if (data == null) {
            //Display an error
            return;
        }
        InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
        //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
    }
}