从图库中选择多个图像

时间:2017-07-10 08:29:44

标签: android

我正在开发一个应用程序,将多个图像添加到ImageView中。现在我可以从谷歌照片应用程序中选择图像,但不能从图库中选择。我得到的异常如下: - java.io.FileNotFoundException:/storage/emulated/0/DCIM/Camera/IMG_20170708_133105.jpg:open failed:EACCES(Permission denied)。任何人都可以建议我在我的代码中缺少什么

这是我的活动

public class MainActivity extends AppCompatActivity {
      Button camera, galary;
      ImageView imageView;
      private static final int CAMERA_PIC_REQUEST = 1337;
      private static final int SELECT_IMAGE = 1338;
      private Bitmap bitmap;
      ArrayList<Uri> imagesUriArrayList ;
      ListView listView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    camera = (Button) findViewById(R.id.button_camera);
    galary = (Button) findViewById(R.id.button_galary);
    imageView = (ImageView) findViewById(R.id.image);

    listView = (ListView) findViewById(R.id.listView);
    listView.setVisibility(View.INVISIBLE);

    camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    });
    galary.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

            startActivityForResult(intent, SELECT_IMAGE);
        }
    });



}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK && null != data) {
        bitmap = (Bitmap) data.getExtras().get("data");

        imageView.setImageBitmap(bitmap);
    }
    if (requestCode == SELECT_IMAGE && resultCode == RESULT_OK && null != data) {
        imagesUriArrayList =  new ArrayList();
        for (int i = 0; i < data.getClipData().getItemCount(); i++) {
            imagesUriArrayList.add(data.getClipData().getItemAt(i).getUri());
        }
        Log.e("SIZE", imagesUriArrayList.size() + "");
        listView.setVisibility(View.VISIBLE);


        DataAdapter adapter = new DataAdapter(MainActivity.this,MainActivity.this, imagesUriArrayList);
        listView.setAdapter(adapter);

        }

     else if (resultCode == this.RESULT_CANCELED) {
        Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
    }
}}

这是我的适配器类

public class DataAdapter extends ArrayAdapter{
     ArrayList<Uri> imagesUriArrayList;
     Activity mainActivity;
     ImageView imageview;
     Bitmap bitmap = null;
     Context context;
public DataAdapter(Context context, Activity mainActivity, ArrayList imagesUriArrayList) {
    super(context,R.layout.listview,imagesUriArrayList);
    this.imagesUriArrayList = imagesUriArrayList;
    this.mainActivity = mainActivity;
    this.context = context;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) mainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listview, parent, false);
        imageview = (ImageView) convertView.findViewById(R.id.imageView);
    }

    Uri selectedImage = imagesUriArrayList.get(position);

    try {
        bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(),selectedImage);
    } catch (IOException e) {
        e.printStackTrace();
    }

    imageview.setImageBitmap(bitmap);



    return  convertView;
}}

2 个答案:

答案 0 :(得分:3)

试试这个

 Intent intent = new Intent();
 intent.setType("image/*");
 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent, "Select Picture"), 5);

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

        if (requestCode == PICK_IMAGE_MULTIPLE) {

            Log.e("++data", "" + data.getClipData().getItemCount());// Get count of image here.

            Log.e("++count", "" + data.getClipData().getItemCount());

            if (data.getClipData().getItemCount() > 5) {
                adapter.notifyDataSetChanged();
                Snackbar snackbar = Snackbar
                        .make(findViewById(R.id.btnSelectImg), "You can not select more than 15 images", Snackbar.LENGTH_LONG)
                        .setAction("RETRY", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent();
                                intent.setType("image/*");
                                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                startActivityForResult(Intent.createChooser(intent, "Select Picture"), 5);
                            }
                        });
                snackbar.setActionTextColor(Color.BLUE);
                View sbView = snackbar.getView();
                TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                textView.setTextColor(Color.RED);
                snackbar.show();

            } else {
                imagesUriArrayList.clear();

                for (int i = 0; i < data.getClipData().getItemCount(); i++) {
                    imagesUriArrayList.add(data.getClipData().getItemAt(i).getUri());
                }
                Log.e("SIZE", imagesUriArrayList.size() + "");
                adapter = new DataAdapter(MainActivity.this, imagesUriArrayList);
                imageresultRecycletview.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

        }
    }

}

像这样加载图片

Bitmap bitmap=BitmapFactory.decodeFile(imagesUriArrayList.get(position));
bitmap = Bitmap.createScaledBitmap(bitmap,150, 150, true);
resultImageview.setImageBitmap(bitmap);

如果有任何疑问,请询问我

答案 1 :(得分:0)

用于一张一张的图片选择

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {

    private Context context;
    private ImageAction listener;
    private ArrayList<ImageModel> arrayList;

    public ImageAdapter(Context context, ImageAction listener, ArrayList<ImageModel> arrayList) {
        this.context = context;
        this.listener = listener;
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public ImageAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_image, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ImageAdapter.ViewHolder viewHolder, final int position) {
        if (position == getItemCount() - 1) {
            viewHolder.ivImage.setVisibility(View.GONE);
            viewHolder.ivAdd.setVisibility(View.VISIBLE);
            viewHolder.ivDot.setVisibility(View.GONE);
            viewHolder.ivEdit.setVisibility(View.GONE);
            viewHolder.ivClose.setVisibility(View.GONE);
            viewHolder.ivUpload.setVisibility(View.GONE);
        } else {
            viewHolder.ivAdd.setVisibility(View.GONE);
            viewHolder.ivDot.setVisibility(View.VISIBLE);
            viewHolder.ivImage.setVisibility(View.VISIBLE);
            Glide.with(context).
                    load(arrayList.get(position).getImage())
                    .into(viewHolder.ivImage);
        }

        viewHolder.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAddImage();
                }
            }
        });

        viewHolder.ivUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    hideOptions(viewHolder);
                    listener.onEditImage(position);
                }
            }
        });

        viewHolder.ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    hideOptions(viewHolder);
                    listener.onDeleteImage(position);
                }
            }
        });

        viewHolder.ivDot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleVisibility(viewHolder.ivClose);
                toggleVisibility(viewHolder.ivUpload);
            }
        });
    }

    private void toggleVisibility(View view) {
        if (view.getVisibility() == View.VISIBLE) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }

    private void hideOptions(ViewHolder viewHolder) {
        viewHolder.ivClose.setVisibility(View.GONE);
        viewHolder.ivUpload.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return arrayList.size() + 1;
    }

    public void onItemAction(String action, int position, String imagePath) {
        switch (action.toLowerCase().trim()) {
            case "add":
                arrayList.add(new ImageModel(imagePath));
                notifyDataSetChanged();
                break;

            case "edit":
                arrayList.get(position).setImage(imagePath);
                notifyDataSetChanged();
                break;

            case "delete":
                arrayList.remove(position);
                notifyDataSetChanged();
                break;
        }
    }

    public ArrayList<ImageModel> getImageList() {
        return arrayList;
    }

    public interface ImageAction {
        void onAddImage();

        void onEditImage(int position);

        void onDeleteImage(int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private AppCompatImageView ivImage, ivAdd, ivDot, ivClose, ivEdit, ivUpload;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.ivImage);
            ivAdd = itemView.findViewById(R.id.ivAdd);
            ivDot = itemView.findViewById(R.id.ivDot);
            ivClose = itemView.findViewById(R.id.ivClose);
            ivEdit = itemView.findViewById(R.id.ivEdit);
            ivUpload = itemView.findViewById(R.id.ivUpload);
        }
    }
}

ImageModel.java

public class ImageModel {
    String image;

    public ImageModel(String image) {
        this.image = image;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

row_image.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="130dp"
    android:layout_height="150dp"
    android:orientation="vertical"
    app:cardCornerRadius="10dp"
    app:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/ivImage"
            android:layout_width="match_parent"
            android:layout_height="110dp"
            android:background="#EAEAEA"
            android:scaleType="fitXY"
            android:visibility="visible" />

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/ivAdd"
            android:layout_width="match_parent"
            android:layout_height="110dp"
            android:background="@color/viewlinecolor"
            android:padding="40dp"
            android:src="@drawable/ic_add"
            android:visibility="gone" />

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/ivDot"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"
            android:src="@drawable/ic_dot" />

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/ivClose"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="72dp"
            android:layout_marginRight="72dp"
            android:layout_marginBottom="20dp"
            android:src="@drawable/ic_remove"
            android:visibility="gone" />

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/ivEdit"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="60dp"
            android:layout_marginRight="60dp"
            android:layout_marginBottom="70dp"
            android:src="@drawable/ic_edit"
            android:visibility="invisible" />

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/ivUpload"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="90dp"
            android:src="@drawable/ic_upload"
            android:visibility="gone" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

使用

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvImage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Java

private ImageAdapter imageAdapter;
private RecyclerView rvImage;
private ArrayList<ImageModel> arrayList;
private ImageAdapter imageAdapter;

implements ImageAdapter.ImageAction

rvImage = findViewById(R.id.rvImage);
rvImage.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
rvImage.setHasFixedSize(false);
arrayList = new ArrayList<>();
imageAdapter = new ImageAdapter(this, this, arrayList);
rvImage.setAdapter(imageAdapter);

@Override
public void onAddImage() {
    imageAdapter.onItemAction("add", -1, uri.selectedImageUri.getPath());
}

@Override
public void onEditImage(int position) {
    imageAdapter.onItemAction("edit", positionOfImage, uri.selectedImageUri.getPath());
}

@Override
public void onDeleteImage(int position) {
    imageAdapter.onItemAction("delete", position, "");
}

获取图片列表

imageAdapter.getImageList();