android intent multi

编程入门 行业动态 更新时间:2024-10-07 05:22:46

<a href=https://www.elefans.com/category/jswz/34/1771384.html style=android intent multi"/>

android intent multi

从设备中选择图像

//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);

更多推荐

android intent multi

本文发布于:2024-02-28 15:16:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1769658.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:android   intent   multi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!