我得到了用户可以将产品添加到db(名称/详细信息/图像)的活动。用户可以从设备库中设置图像,也可以只保留样本图像。我可以在其他活动中看到所有产品的名称列表,在另一个活动中,我应该看到所有关于选择产品的信息,以便图像/名称详细信息。
我尝试了几种将imageview
的内容转换为byte[]
的方法。我简要介绍了所有这些:
首先:
Bitmap bit = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
image = bitmapToByteConvert(bit);
这种方式在db中创建行的过程中不会产生任何错误。如果我想看到产品列表一切正常,但当我想看到所有信息时,图像不会出现。如果我添加样本一或来自图库,则没有区别。
第二
image = bitmapToByteConvert(((BitmapDrawable)picture.getDrawable()).getBitmap());
如果我用样品图像存储产品一切都很好,我没有任何存储问题,我可以看到所有产品名称的列表,当我选择一个时,我可以看到有关它的所有信息,所以我可以看到名称/图像/细节。当我处理来自图库的图像时出现错误,我也可以存储它,但当我尝试查看所有名称的列表时,我得到错误与游标连接
11-08 14:25:36.381: E/AndroidRuntime(3414): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
第三
picture.setDrawingCacheEnabled(true);
picture.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
picture.layout(0, 0, picture.getMeasuredWidth(), picture.getMeasuredHeight());
picture.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(picture.getDrawingCache());
picture.setDrawingCacheEnabled(false);
image = bitmapToByteConvert(b);
最后一个,当我处理样本时Image一切都很好,但是当我想用图库中的图像保存产品时我得到了这个错误
11-08 14:36:02.026: E/AndroidRuntime(3919): FATAL EXCEPTION: main
11-08 14:36:02.026: E/AndroidRuntime(3919): java.lang.IllegalStateException:
Could not execute method of the activity
11-08 14:36:02.026: E/AndroidRuntime(3919): at
android.view.View$1.onClick(View.java:3633)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
android.view.View.performClick(View.java:4240)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
android.view.View$PerformClick.run(View.java:17721)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
android.os.Handler.handleCallback(Handler.java:730)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
android.os.Handler.dispatchMessage(Handler.java:92)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
android.os.Looper.loop(Looper.java:137)
11-08 14:36:02.026: E/AndroidRuntime(3919): at a
android.app.ActivityThread.main(ActivityThread.java:5103)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
java.lang.reflect.Method.invokeNative(Native Method)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
java.lang.reflect.Method.invoke(Method.java:525)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
dalvik.system.NativeStart.main(Native Method)
11-08 14:36:02.026: E/AndroidRuntime(3919): Caused by:
java.lang.reflect.InvocationTargetException
11-08 14:36:02.026: E/AndroidRuntime(3919): at
java.lang.reflect.Method.invokeNative(Native Method)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
java.lang.reflect.Method.invoke(Method.java:525)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
android.view.View$1.onClick(View.java:3628)
11-08 14:36:02.026: E/AndroidRuntime(3919): ... 11 more
11-08 14:36:02.026: E/AndroidRuntime(3919): Caused by:
java.lang.NullPointerException
11-08 14:36:02.026: E/AndroidRuntime(3919): at
android.graphics.Bitmap.createBitmap(Bitmap.java:505)
11-08 14:36:02.026: E/AndroidRuntime(3919): at
com.example.nfc_friend.AddProductToDB.AddTheProductMethod(AddProductToDB.java:79)
11-08 14:36:02.026: E/AndroidRuntime(3919): ... 14 more
我无法将用户选择的图像添加到app中的资源文件夹中导致只读,但我认为问题是从图库中拍摄的图片只是放在imageView中,我没有保存在应用程序文件夹或类似的smth。
将从图库中获取的图像数据正确放入数据库的方法是什么?