为所有应用程序的私有映像创建路径的ArrayList

时间:2015-07-15 22:49:12

标签: android android-camera android-gallery

我需要一些关于如何访问外部存储以创建我所有应用程序私有映​​像的ArrayList的帮助。

在我的应用程序中,我可以使用相机拍摄照片并使用以下代码立即显示该图像:

public class CameraFragment extends Fragment {
public static String TAG = "Camera Fragment";

private static final int REQUEST_CODE = 1;
private Bitmap mImage;
private Uri mImageUri;
private ImageView mImageView;

public CameraFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_camera, container, false);
    Button cameraButton = (Button) rootView.findViewById(R.id.button_camera);
    mImageView = (ImageView) rootView.findViewById(R.id.results);

    cameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            takePhoto(v);
        }
    });

    return rootView;
}

private void takePhoto(View v) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                                                                System.currentTimeMillis() + "_pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    mImageUri = Uri.fromFile(photo);
    startActivityForResult(intent, REQUEST_CODE);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case 1:
            if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = mImageUri;
            ContentResolver contentResolver = getActivity().getContentResolver();
            contentResolver.notifyChange(selectedImage, null);

            try {
                mImage = MediaStore.Images.Media.getBitmap(contentResolver, selectedImage);
                mImageView.setImageBitmap(mImage);
                Toast.makeText(getActivity(), selectedImage.toString(), Toast.LENGTH_LONG).show();
                galleryAddPic(selectedImage);
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), "failed to load", Toast.LENGTH_LONG).show();
            }
        }
    }
}

private void galleryAddPic(Uri selectedImage) {
    Log.v(TAG, "adding image to gallery");
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    mediaScanIntent.setData(selectedImage);
    getActivity().sendBroadcast(mediaScanIntent);
}

现在,我想在另一项活动中显示此应用程序捕获的所有图像。我有这个POJO模型来存储它们被拉出时的图像:

public class ImageItem {
private Bitmap image;
private String title;

public ImageItem(Bitmap image, String title) {
    super();
    this.image = image;
    this.title = title;
}

public Bitmap getImage() {
    return image;
}

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

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

}

我想在外部存储空间中使用我的图像ArrayList<ImageItem>然后传递给我的GridViewAdapter,但我不知道从哪里开始。

以下是我的GridViewAdapter供参考:

public class GridViewAdapter extends ArrayAdapter {
private Context mContext;
private int mLayoutResourceId;
private ArrayList mData = new ArrayList();

public GridViewAdapter(Context context, int layoutResourceId, ArrayList data) {
    super(context, layoutResourceId, data);
    mLayoutResourceId = layoutResourceId;
    mContext = context;
    mData = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        row = inflater.inflate(mLayoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.image = (ImageView) row.findViewById(R.id.image);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    ImageItem item = (ImageItem) mData.get(position);
    holder.imageTitle.setText(item.getTitle());
    holder.image.setImageBitmap(item.getImage());
    return row;
}

static class ViewHolder {
    TextView imageTitle;
    ImageView image;
}

0 个答案:

没有答案