图像无法在网格中显示 BitmapFactory.decodefile:无法解码流:java.lang.NullPointerException?

Image not able to display in grid BitmapFactory.decodefile :Unable to decode stream: java.lang.NullPointerException?

(还有 12 个类似的问题,但 none 个问题回答了我的问题。因为它属于代码异常,并非每个重复的问题都有相同的问题)。

我正在尝试从我创建的绘图应用程序的自定义文件夹中获取图像。它已经获取所有文件并获取 custom_adapter 中的所有图像但是当我使用位图解码时它会抛出错误 Unable to decode stream: java.lang.NullPointerException.

代码如下:

MainActivity.class

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.collection_grid);

        String path = Environment.getExternalStorageDirectory().toString() + "/" + getString(R.string.Drawing);
        Log.d("Files", "Path: " + path);
        File f = new File(path);
        File file[] = f.listFiles();
        Log.d("Files", "Size: " + file.length);
        for (int i=0; i < file.length; i++)
        {
            Log.d("Files", "FileName:" + file[i].getName());
        }
        String [] FilePathStrings = (new String[file.length]);
        GridView g1=(GridView)findViewById(R.id.grid);

        g1.setAdapter(new Custom_grid(Grid_Collection.this,FilePathStrings));
    }

Custom_Adapter.class

public class Custom_grid extends BaseAdapter {

    String [] result;
    private static LayoutInflater inflater=null;
    Context context;

    public Custom_grid(Grid_Collection grid_collection,String [] file) {
        result= file;
        context = grid_collection;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
//        Log.e("LIST SIZE", "" + result.length);
        return result.length;
    }

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

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

    class Holder{
        ImageView iv;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        View vi=convertView;

        if(convertView == null){

            vi = inflater.inflate(R.layout.imageview,null);

            holder = new Holder();

            holder.iv= (ImageView)vi.findViewById(R.id.image);

            vi.setTag(holder);
        }
        else {
            holder = (Holder) vi.getTag();
        }

        Log.e("LIST POSITION", "" + result[position]);
        Bitmap bmp = BitmapFactory.decodeFile(result[position]);

        holder.iv.setImageBitmap(bmp);



    return vi;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }
}

为什么显示错误?代码没问题

LOGCAT::

10-17 13:59:38.852 19817-19817/com.example.dell.drawdemo D/AbsListView: checkAbsListViewlLogProperty get invalid command
10-17 13:59:38.853 19817-19817/com.example.dell.drawdemo D/Files: Path: /storage/emulated/0/Drawing App
10-17 13:59:38.862 19817-19817/com.example.dell.drawdemo D/Files: Size: 13
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:984f456b-91ee-4cd7-893e-faeb44fcc1c1.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:79fdc1fa-c5bb-4e88-aa26-94563b0d72bd.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:a4537e3d-c708-45e5-82fd-8c43e48878d3.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:cdaa2564-6db0-4b77-ad33-c06eb37978a2.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:41d782a1-7682-4e69-a0e3-7bf3adc9dc3f.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:bd06264a-d91e-43b2-b17b-e7d2746642cc.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:d1fcb76f-c90b-44ac-98f2-03d0dd255532.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:86c8f75d-4532-4100-b239-e3e67fe45dec.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:811e7856-0dc7-4a5a-ac4c-371f39d5a16f.png
10-17 13:59:38.864 19817-19817/com.example.dell.drawdemo D/Files: FileName:48c634e3-a3d0-43d6-9387-9ce3dba7c874.png
10-17 13:59:38.864 19817-19817/com.example.dell.drawdemo D/Files: FileName:6057691d-f96e-4554-921e-316cda99b034.png
10-17 13:59:38.864 19817-19817/com.example.dell.drawdemo D/Files: FileName:1c7674be-860c-4e76-a5bb-21648bd3d9ea.png
10-17 13:59:38.864 19817-19817/com.example.dell.drawdemo D/Files: FileName:3665b64a-e2eb-4d81-ab62-d40ab0cc74ac.png
10-17 13:59:38.865 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-AM_ON_RESUME_CALLED ActivityRecord{42434500 token=android.os.BinderProxy@42433c48 {com.example.dell.drawdemo/com.example.dell.drawdemo.Grid_Collection}}
10-17 13:59:38.865 19817-19817/com.example.dell.drawdemo V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.impl.PhoneWindow$DecorView{42435aa0 I.E..... R.....ID 0,0-0,0}
10-17 13:59:38.870 19817-19817/com.example.dell.drawdemo V/PhoneWindow: DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{42456a48 com.example.dell.drawdemo/com.example.dell.drawdemo.Grid_Collection,ident = 1}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{42435aa0 V.E..... R.....ID 0,0-0,0}
10-17 13:59:38.872 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{42434500 token=android.os.BinderProxy@42433c48 {com.example.dell.drawdemo/com.example.dell.drawdemo.Grid_Collection}}
10-17 13:59:38.891 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.893 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.927 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x62a76d68) (w:544, h:960, f:1)
10-17 13:59:38.928 19817-19817/com.example.dell.drawdemo I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
10-17 13:59:38.928 19817-19817/com.example.dell.drawdemo I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
10-17 13:59:38.928 19817-19817/com.example.dell.drawdemo I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3
10-17 13:59:38.929 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: close handle(0x62a76d68) (w:544 h:960 f:1)
10-17 13:59:38.932 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x62a79b68) (w:544, h:960, f:1)
10-17 13:59:38.934 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: setViewport 540x960 <0x62a79cf0>
10-17 13:59:38.935 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.935 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.938 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.938 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.941 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.941 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.944 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.944 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.947 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.948 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.951 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.951 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.954 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.954 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.957 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.957 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.960 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.960 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.963 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.963 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.966 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.966 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.969 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.969 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.972 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.972 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.976 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.976 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.985 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:38.985 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:38.989 19817-19817/com.example.dell.drawdemo D/AbsListView: onWindowFocusChanged: hasWindowFocus=true, this=android.widget.GridView{42439dc8 VFED.VC. .F....I. 0,0-540,922 #7f0c0067 app:id/grid}
10-17 13:59:38.990 19817-19817/com.example.dell.drawdemo V/InputMethodManager: onWindowFocus: android.widget.GridView{42439dc8 VFED.VC. .F....I. 0,0-540,922 #7f0c0067 app:id/grid} softInputMode=272 first=true flags=#1810100
10-17 13:59:38.990 19817-19817/com.example.dell.drawdemo V/InputMethodManager: START INPUT: android.widget.GridView{42439dc8 VFED.VC. .F....I. 0,0-540,922 #7f0c0067 app:id/grid} ic=null tba=android.view.inputmethod.EditorInfo@42470a08 controlFlags=#105
10-17 13:59:39.002 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:39.002 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:39.008 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x6207c688) (w:544, h:960, f:1)
10-17 13:59:39.016 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:39.016 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:39.030 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x62f4e0b8) (w:544, h:960, f:1)
10-17 13:59:39.032 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: Flushing caches (mode 0)
10-17 13:59:39.033 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: close handle(0x618d6d18) (w:544 h:960 f:1)
10-17 13:59:39.033 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: close handle(0x5d127550) (w:544 h:960 f:1)
10-17 13:59:39.034 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: close handle(0x62873990) (w:544 h:960 f:1)
10-17 13:59:39.034 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: close handle(0x62f4e0b8) (w:544 h:960 f:1)
10-17 13:59:39.346 19817-19817/com.example.dell.drawdemo V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =ViewRoot{424234f8 com.example.dell.drawdemo/com.example.dell.drawdemo.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{42316338 I.E..... R....... 0,0-540,960}
10-17 13:59:39.346 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-STOP_ACTIVITY_HIDE handled : 0 / android.os.BinderProxy@42303440
10-17 13:59:40.102 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x5d12efa8) (w:544, h:960, f:1)
10-17 13:59:40.104 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.105 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.119 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x62f4e0b8) (w:544, h:960, f:1)
10-17 13:59:40.121 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.122 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.135 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.136 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.152 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.153 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.169 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.169 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.187 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.188 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.201 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.202 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.218 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.218 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.235 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.235 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.251 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.252 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.268 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.269 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.286 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.287 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.301 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.302 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.318 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.318 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.336 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.337 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.351 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.351 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:54.746 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{42434500 token=android.os.BinderProxy@42433c48 {com.example.dell.drawdemo/com.example.dell.drawdemo.Grid_Collection}}
10-17 13:59:54.781 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@42433c48
10-17 13:59:54.812 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@42433c48
10-17 13:59:55.024 19817-19817/com.example.dell.drawdemo V/InputMethodManager: START INPUT: android.widget.GridView{42439dc8 VFED.VC. .F...... 0,0-540,922 #7f0c0067 app:id/grid} ic=null tba=android.view.inputmethod.EditorInfo@42481718 controlFlags=#100


也许这会对你有所帮助:)
有:

File file[] = f.listFiles();

您将获得在日志中输出的文件列表。
比你实例化 FilePathStrings: (但现在它是一个空数组!)

String [] FilePathStrings = (new String[file.length]);

请在调用 setAdapter(...) 之前添加此行

    ...
    for (int i=0; i < file.length; i++){
        FilePathStrings[i] = file[i].getAbsolutePath();
    }
    ...

请编辑您的代码并告诉我这是否解决了问题。