如何在android中上传图库

use*_*266 15 android android-layout

我想将手机库中的图像上传到我的应用程序中.在我的应用程序中有一个名为upload的按钮.当我点击按钮,它应该移动到画廊和画廊,如果我选择图像,所选图像应该在应用程序中显示为缩略图.我想从我的应用程序中的库中上传10个图像.

Dhr*_*ola 20

单击图库按钮,启动startActivityForResult,如下所示:

startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);
Run Code Online (Sandbox Code Playgroud)

因此,public static final int GET_FROM_GALLERY = 3;在onActivityResult中检测GET_FROM_GALLERY(这是一个静态int,您选择的任何请求编号,例如).

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    //Detects request codes
    if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
        Uri selectedImage = data.getData();
        Bitmap bitmap = null;
        try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
        } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ses*_*nay 7

要查看图库:

Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(Intent.createChooser(intent, "Select Picture"),REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)

并在您的应用程序中使用它:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  try {
   switch (requestCode) {

   case REQUEST_CODE:
    if (resultCode == Activity.RESULT_OK) {
     //data gives you the image uri. Try to convert that to bitmap
     break;
    } else if (resultCode == Activity.RESULT_CANCELED) {
     Log.e(TAG, "Selecting picture cancelled");
    }
    break;
   }
  } catch (Exception e) {
   Log.e(TAG, "Exception in onActivityResult : " + e.getMessage());
  }
 }
Run Code Online (Sandbox Code Playgroud)


小智 5

这是要走的路:

startActivityForResult(
  new Intent(
    Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
  ),
  GET_FROM_GALLERY
);
Run Code Online (Sandbox Code Playgroud)