android intent multi_pick 自定在哪里,java - 使用Intents NullPointerException从Android的照片库中选择多个图像 - 堆栈内存溢出...

从设备中选择图像

//Uri to store the image uri

private List filePath;

public String path[];

public static List listOfImage;

//Image request code

private int PICK_IMAGE_REQUEST = 1;

private int PICK_IMAGE_REQUEST_MULTI = 2;

// select multiple image

private void pickImages() {

filePath = new ArrayList<>();

listOfImage = new ArrayList<>();

Intent intent;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

intent = new Intent();

intent.setType("image/*");

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

intent.setAction(Intent.ACTION_GET_CONTENT);

intent.setAction(Intent.ACTION_OPEN_DOCUMENT);

intent.addCategory(Intent.CATEGORY_OPENABLE);

startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST_MULTI);

}else {

intent = new Intent();

intent.setType("image/*");

intent.setAction(Intent.ACTION_GET_CONTENT);

intent.setAction(Intent.ACTION_OPEN_DOCUMENT);

intent.addCategory(Intent.CATEGORY_OPENABLE);

startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

}

}

活动结果

//handling the image chooser activity result

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE_REQUEST_MULTI && resultCode == RESULT_OK && data != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

ClipData imagesPath = data.getClipData();

if (imagesPath != null) {

path = new String[imagesPath.getItemCount()];

for (int i = 0; i < imagesPath.getItemCount(); i++) {

filePath.add(imagesPath.getItemAt(i).getUri());

path[i] = getPath(imagesPath.getItemAt(i).getUri());

ImageBeen imageBeen = new ImageBeen(imagesPath.getItemAt(i).getUri());

imageBeen.setPath(path[i]);

listOfImage.add(imageBeen);

initViewPager();

}

}else {

path = new String[1];

path[0] = getPath(data.getData());

ImageBeen imageBeen = new ImageBeen(data.getData());

imageBeen.setPath(path[0]);

listOfImage.add(imageBeen);

initViewPager();

}

} else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {

path = new String[1];

path[0] = getPath(data.getData());

ImageBeen imageBeen = new ImageBeen(data.getData());

imageBeen.setPath(path[0]);

listOfImage.add(imageBeen);

initViewPager();

}

}

使用图片URI来定位图片路径

//method to get the file path from uri

public String getPath(Uri uri) {

Cursor cursor = getContentResolver().query(uri, null, null, null, null);

String path = null;

if(cursor!=null) {

if (cursor.moveToFirst()) {

String document_id = cursor.getString(0);

document_id = document_id.substring(document_id.lastIndexOf(":") + 1);

cursor.close();

cursor = getContentResolver().query(

MediaStore.Images.Media.EXTERNAL_CONTENT_URI,

null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);

cursor.moveToFirst();

path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

cursor.close();

return path;

}

}

return path;

}

这是图像bean,其中存储了图像URI和图像路径

public class ImageBeen {

private Uri fileUri;

private String path;

public ImageBeen(Uri fileUri)

{

this.fileUri=fileUri;

}

public String getPath() {

return path;

}

public void setPath(String path)

{

this.path=path;

}

public Uri getFileUri() {

return fileUri;

}

}

对于显示图像,请使用毕加索

编译'com.squareup.picasso:picasso:2.5.2'

Picasso.with(context)

.load(imageBeen.getFileUri())

.placeholder(R.drawable.not_vailable)

.error(R.drawable.not_vailable)

.into(holder.image);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值