Android:从GridView中删除图像

时间:2015-10-28 05:39:07

标签: android

我尝试通过捕获的相机和图库按钮监听器在网格视图中显示图像。之后,从存储它们的SD卡中删除选定的多个图像,并使用复选框从网格视图中删除。但是当我从相机或图库中捕获图像时,网格视图中没有显示图像。有人帮我解决了这个问题。谢谢提前。

这是我的活动代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try
        {
            if (requestCode == 1010 && resultCode == RESULT_OK)
            {
                String newString = data.getExtras().getString("STRING_I_NEED");
                Log.e("onActivityResult  ", "newString = " + newString);

                audioSize.add(newString);
                int audioCount = audioSize.size();
                txt_countAudio.setText(Integer.toString(audioCount));

                Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.resize_audio_control);
                BitmapItems item = new BitmapItems();
                item.setBitmap(largeIcon);
                imgItemArray.add(item);
                //gridAdapter = new GridView_Adapter(this, R.layout.add_post_grid_item_layout, picsName);
                //gridView.setAdapter(gridAdapter);
               // gridAdapter.notifyDataSetChanged();
            }

            if (requestCode == CAMERA_REQUEST)
            {
                SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
                String picformat = strVesselId + "_" + s.format(new Date()) + ".jpeg";
                Log.e(" picformat ", " = " + picformat);

                Bitmap photoBitMap = (Bitmap) data.getExtras().get("data");
                String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                String path = baseDir + "/classnkk_audio/" + picformat;
                Log.e("path "," = "+path);
                File myPath = new File(path);
                try
                {
                    FileOutputStream fileOutputStream = new FileOutputStream(myPath);
                    photoBitMap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
                    fileOutputStream.flush();
                    fileOutputStream.close();

                    if (myPath.isDirectory())
                    {
                        File file = new File(path);

                        if (file.isDirectory()) {
                            listFile = file.listFiles();

                            for (int i = 0; i < listFile.length; i++) {

                                fileName.add(listFile[i].getAbsolutePath());

                            }
                        }
                    }
                }
                catch (FileNotFoundException e)
                {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


                Log.e("fileName.size() ", " = " + fileName.size());
                gridAdapter = new GridView_Adapter();
                gridView.setAdapter(gridAdapter);
                gridAdapter.notifyDataSetChanged();

                int itemCount = imageSize.size();
                Log.e("itemCount ", " = " + itemCount);
                textTotalImages.setText(Integer.toString(itemCount));
            }
            if (requestCode == RESULT_LOAD_IMG)
            {
                try
                {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    imgDecodableString = cursor.getString(columnIndex);
                    String[] finalPath = imgDecodableString.split("/");
                    final String splitString = finalPath[finalPath.length - 1];
                    Log.e(" splitString ", " = " + splitString);
                    Log.e("imgDecodableString ", " = " + imgDecodableString);
                    cursor.close();

                    Bitmap bmp = BitmapFactory.decodeFile(imgDecodableString);
                    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                    String path = baseDir + "/classnkk_audio/" + splitString;
                    File myPath = new File(path);
                    Log.e("myPath ", " From gallery = " + myPath);
                    try
                    {
                        FileOutputStream fileOutputStream = new FileOutputStream(myPath);
                        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
                        fileOutputStream.flush();
                        fileOutputStream.close();

                        if (myPath.isDirectory())
                        {
                            if (myPath.isDirectory())
                            {
                                fileName.add(path);
                            }
                        }
                    }
                    catch (FileNotFoundException e)
                    {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }



                    Log.e("fileName.size() ", " = " + fileName.size());
                    gridAdapter = new GridView_Adapter();
                    gridView.setAdapter(gridAdapter);
                    gridAdapter.notifyDataSetChanged();

                    int itemCount = imageSize.size();
                    Log.e("itemCount ", " = " + itemCount);
                    textTotalImages.setText(Integer.toString(itemCount));

                } catch (Exception e) {
                    Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我的GridView_Adapter代码

class GridView_Adapter extends BaseAdapter {

        private LayoutInflater mInflater;

        public GridView_Adapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }


        public int getCount() {
            return fileName.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.add_post_grid_item_layout, null);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Bitmap myBitmap = BitmapFactory.decodeFile(fileName.get(position));
            Log.e("myBitmap ", " = " + myBitmap);
            holder.image.setImageBitmap(myBitmap);
            final int pos = position;
            holder.checkbox.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (!picsName.contains((String) fileName.get(pos)))
                    {
                        picsName.add((String) fileName.get(pos));
                        Log.e("","");
                    }
                    else
                    {
                        picsName.remove((String) fileName.get(pos));
                        Log.e("Remove ", " Image !!!!");
                    }
                }
            });
            return convertView;
        }

        class ViewHolder
        {
            ImageView image;
            CheckBox checkbox;
            int id;
        }
    }
}

这是我的Log cat Error

 2886-5037/? E/ThumbnailUtils﹕ java.io.FileNotFoundException: /storage/emulated/0/classnkk_images/06662269-43d0-4e84-870b-b6ae0488ce2d.png: open failed: ENOENT (No such file or directory)
            at libcore.io.IoBridge.open(IoBridge.java:456)
            at java.io.FileInputStream.<init>(FileInputStream.java:76)
            at java.io.FileInputStream.<init>(FileInputStream.java:103)
            at android.media.ThumbnailUtils.createImageThumbnail(ThumbnailUtils.java:103)
            at android.provider.MediaStore$InternalThumbnails.getThumbnail(MediaStore.java:729)
            at android.provider.MediaStore$Images$Thumbnails.getThumbnail(MediaStore.java:1060)
            at b.a(PG:55262)
            at idn.h(PG:24642)
            at htu.c(PG:814)
            at idn.c(PG:1379)
            at jpi.handleMessage(PG:293)
            at android.os.Handler.dispatchMessage(Handler.java:98)
            at android.os.Looper.loop(Looper.java:135)
            at android.os.HandlerThread.run(HandlerThread.java:61)
            at jpi.run(PG:280)
     Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
            at libcore.io.Posix.open(Native Method)
            at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
            at libcore.io.IoBridge.open(IoBridge.java:442)
            at java.io.FileInputStream.<init>(FileInputStream.java:76)
            at java.io.FileInputStream.<init>(FileInputStream.java:103)
            at android.media.ThumbnailUtils.createImageThumbnail(ThumbnailUtils.java:103)
            at android.provider.MediaStore$InternalThumbnails.getThumbnail(MediaStore.java:729)
            at android.provider.MediaStore$Images$Thumbnails.getThumbnail(MediaStore.java:1060)
            at b.a(PG:55262)
            at idn.h(PG:24642)
            at htu.c(PG:814)
            at idn.c(PG:1379)
            at jpi.handleMessage(PG:293)
            at android.os.Handler.dispatchMessage(Handler.java:98)
            at android.os.Looper.loop(Looper.java:135)
            at android.os.HandlerThread.run(HandlerThread.java:61)
            at jpi.run(PG:280)
10-28 11:07:42.089    6091-6091/? E/splitString﹕ = ae203581-b88a-4ce8-a8a3-6c25f7347f13.png
10-28 11:07:42.089    6091-6091/? E/imgDecodableString﹕ = /storage/emulated/0/classnkk_images/ae203581-b88a-4ce8-a8a3-6c25f7347f13.png
10-28 11:07:42.095    6091-6091/? E/myPath﹕ From gallery = /storage/emulated/0/classnkk_images/ae203581-b88a-4ce8-a8a3-6c25f7347f13.png
10-28 11:07:42.106    6091-6091/? W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at com.example.tazeen.classnkk.AddPost.onActivityResult(AddPost.java:656)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at android.app.Activity.dispatchActivityResult(Activity.java:6135)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at android.app.ActivityThread.deliverResults(ActivityThread.java:3564)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at android.app.ActivityThread.handleSendResult(ActivityThread.java:3611)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at android.app.ActivityThread.access$1400(ActivityThread.java:147)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
10-28 11:07:42.107    6091-6091/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

1 个答案:

答案 0 :(得分:0)

FileNotFoundException就在那里。检查一下它是否保存文件或在您的位置。 或更新全班。