因此,我有一个应用程序,用户可以拍摄照片并将其存储在本地。图像名称存储在我的SQLite数据库中,以供将来参考以调用该照片。现在,我已经追踪到将位图图像设置为imageview的问题。这是我的代码:

    private void displayListView() {
    // TODO Auto-generated method stub

    Cursor cursor = info.getListData();
    String[] listViewColumns = new String[] { Database.KEY_BREWNAME, Database.KEY_BREWERY, Database.KEY_RATING,
            Database.KEY_DATEIMAGENAME, };
    int[] dataToListView = new int[] { R.id.tvBrewName, R.id.tvBrewery, R.id.tvRating, R.id.ivThumb, };

    dataAdapter = new SimpleCursorAdapter(this, R.layout.entrymain, cursor, listViewColumns, dataToListView, 0);

    ViewBinder viewBinder = new ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            ImageView image = (ImageView)view;
            Log.w(TAG, "******************List Image loaded == " + columnIndex + " **********************");
            try {
                FileInputStream fis = openFileInput(cursor.getString(columnIndex));
                fis.close();
                Log.w(TAG, "******************List Image loaded == fis.close **********************");
                image.setImageBitmap(BitmapFactory.decodeStream(fis));
                Log.w(TAG, "******************List Image loaded == setimage success **********************");

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

        };
    };
    ImageView image = (ImageView)findViewById(R.id.ivThumb);
    viewBinder.setViewValue(image, cursor, cursor.getColumnIndex("date_image_name"));
    dataAdapter.setViewBinder(viewBinder);

    listview.setAdapter(dataAdapter);


该代码在使用ViewBinder并使用log.w时效果很好,我将问题追溯到了image.setImageBitmap(BitmapFactory.decodeStream(fis)),但我不太明白为什么它不会像我一样设置图像我曾在其他地方成功使用过此代码。关于为什么它不会设置的任何想法,我发现这与java.io.IOException有关:读取失败:EBADF(文件号错误),但是我该如何解决?预先感谢!

这是我的错误日志的一部分:

 03-18 10:44:16.281: D/ActivityThread(19380): setTargetHeapUtilization:0.25
03-18 10:44:16.281: D/ActivityThread(19380): setTargetHeapIdealFree:8388608
03-18 10:44:16.281: D/ActivityThread(19380): setTargetHeapConcurrentStart:2097152
03-18 10:44:16.782: I/Adreno200-EGLSUB(19380): <ConfigWindowMatch:2087>: Format RGBA_8888.
03-18 10:44:16.792: E/(19380): <s3dReadConfigFile:75>: Can't open file for reading
03-18 10:44:16.792: E/(19380): <s3dReadConfigFile:75>: Can't open file for reading
03-18 10:44:17.112: D/AbsListView(19380): Get MotionRecognitionManager
03-18 10:44:17.152: W/Start(19380): ******************List Image loaded == 4 **********************
03-18 10:44:17.162: W/Start(19380): ******************List Image loaded == fis.close **********************
03-18 10:44:17.162: W/System.err(19380): java.io.IOException: read failed: EBADF (Bad file number)
03-18 10:44:17.172: W/System.err(19380):    at libcore.io.IoBridge.read(IoBridge.java:442)
03-18 10:44:17.172: W/System.err(19380):    at java.io.FileInputStream.read(FileInputStream.java:179)
03-18 10:44:17.172: W/System.err(19380):    at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168)
03-18 10:44:17.172: W/System.err(19380):    at java.io.BufferedInputStream.read(BufferedInputStream.java:309)
03-18 10:44:17.172: W/System.err(19380):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-18 10:44:17.172: W/System.err(19380):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:619)
03-18 10:44:17.172: W/System.err(19380):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:691)
03-18 10:44:17.172: W/System.err(19380):    at com.ex.beerlog.Start$1.setViewValue(Start.java:78)
03-18 10:44:17.172: W/System.err(19380):    at com.ex.beerlog.Start.displayListView(Start.java:91)
03-18 10:44:17.172: W/System.err(19380):    at com.ex.beerlog.Start.onCreate(Start.java:56)
03-18 10:44:17.172: W/System.err(19380):    at android.app.Activity.performCreate(Activity.java:5048)
03-18 10:44:17.172: W/System.err(19380):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-18 10:44:17.172: W/System.err(19380):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052)
03-18 10:44:17.172: W/System.err(19380):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
03-18 10:44:17.182: W/System.err(19380):    at android.app.ActivityThread.access$700(ActivityThread.java:139)
03-18 10:44:17.182: W/System.err(19380):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
03-18 10:44:17.182: W/System.err(19380):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 10:44:17.182: W/System.err(19380):    at android.os.Looper.loop(Looper.java:137)
03-18 10:44:17.182: W/System.err(19380):    at android.app.ActivityThread.main(ActivityThread.java:4918)
03-18 10:44:17.182: W/System.err(19380):    at java.lang.reflect.Method.invokeNative(Native Method)
03-18 10:44:17.182: W/System.err(19380):    at java.lang.reflect.Method.invoke(Method.java:511)
03-18 10:44:17.182: W/System.err(19380):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
03-18 10:44:17.182: W/System.err(19380):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
03-18 10:44:17.182: W/System.err(19380):    at dalvik.system.NativeStart.main(Native Method)
03-18 10:44:17.182: W/System.err(19380): Caused by: libcore.io.ErrnoException: read failed: EBADF (Bad file number)
03-18 10:44:17.182: W/System.err(19380):    at libcore.io.Posix.readBytes(Native Method)
03-18 10:44:17.182: W/System.err(19380):    at libcore.io.Posix.read(Posix.java:118)
03-18 10:44:17.182: W/System.err(19380):    at libcore.io.BlockGuardOs.read(BlockGuardOs.java:149)
03-18 10:44:17.182: W/System.err(19380):    at libcore.io.IoBridge.read(IoBridge.java:432)
03-18 10:44:17.182: W/System.err(19380):    ... 23 more

最佳答案

您正在尝试从封闭的InputStream解码位图。 fis.close();必须在image.setImageBitmap(BitmapFactory.decodeStream(fis));之后调用

另外,如果您确实将值设置为setViewValue,则必须签入ImageView方法:

ViewBinder viewBinder = new ViewBinder() {
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        if (!(view instanceof ImageView) || view.getId() != R.id.ivThumb) {
            return false;
        }

        // the rest of the code
    };
};


还有最后一件事。您无需显式调用viewBinder.setViewValue(image, cursor, cursor.getColumnIndex("date_image_name"));,因为SimpleAdapter将为您完成此操作。只需删除此行,一切都应该正常工作。

关于android - Android Dev:通过ViewBinder将本地镜像文件插入到从SQLite数据库引用的ListView中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15482344/

10-10 20:14