将Android.net.Uri转换为Java.io.inputStream

时间:2019-04-06 18:04:37

标签: java android

我想通过Android Uri将从手机图库中获取的图像传递给我的Java代码。由于Java.IO需要File或Java URL或java inputStream,因此我如何将android Uri转换为inputStream?

但是每次我遇到例外时。

我尝试使用此代码将我的图像数据的imageUri转换为inputStream。我可以向您保证imageUri具有有效的内容,因为我可以观看imageView中的图片。 如果也许更容易转换为Java URL,我对此也没有任何问题。 谢谢

    InputStream myimage = this.getContentResolver().openInputStream(imageUri);
      LoadImageApp Image = new LoadImageApp( myimage );```



//code that passes content to imageUri.It is working fine.
 protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);

         if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
             imageUri = data.getData();    
             imageView.setImageURI(imageUri);
         }
 }

堆栈跟踪

java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
        at android.view.View.performClick(View.java:4819)
        at android.view.View$PerformClick.run(View.java:20152)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5572)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
        at android.view.View.performClick(View.java:4819) 
        at android.view.View$PerformClick.run(View.java:20152) 
        at android.os.Handler.handleCallback(Handler.java:815) 
        at android.os.Handler.dispatchMessage(Handler.java:104) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5572) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763) 
     Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/imageio/ImageIO;
        at com.example.lib.LoadImageApp.<init>(LoadImageApp.java:66)
        at kostas.sgong.Main_Menu.onClickStart(Main_Menu.java:75)
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
        at android.view.View.performClick(View.java:4819) 
        at android.view.View$PerformClick.run(View.java:20152) 
        at android.os.Handler.handleCallback(Handler.java:815) 
        at android.os.Handler.dispatchMessage(Handler.java:104) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5572) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763) 
     Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.imageio.ImageIO" on path: DexPathList[[zip file "/data/app/kostas.sgong-1/base.apk", zip file "/data/app/kostas.sgong-1/split_lib_dependencies_apk.apk", zip file "/data/app/kostas.sgong-1/split_lib_slice_0_apk.apk", zip file "/data/app/kostas.sgong-1/split_lib_slice_1_apk.apk", zip file "/data/app/kostas.sgong-1/split_lib_slice_2_apk.apk", zip file "/data/app/kostas.sgong-1/split_lib_slice_3_apk.apk", zip file "/data/app/kostas.sgong-1/split_lib_slice_4_apk.apk", zip file "/data/app/kostas.sgong-1/split_lib_slice_5_apk.apk", zip file "/data/app/kostas.sgong-1/split_lib_slice_6_apk.apk", zip file "/data/app/kostas.sgong-1/split_lib_slice_7_apk.apk", zip file "/data/app/kostas.sgong-1/split_lib_slice_8_apk.apk", zip file "/data/app/kostas.sgong-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at com.example.lib.LoadImageApp.<init>(LoadImageApp.java:66) 
        at kostas.sgong.Main_Menu.onClickStart(Main_Menu.java:75) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
        at android.view.View.performClick(View.java:4819) 
        at android.view.View$PerformClick.run(View.java:20152) 
        at android.os.Handler.handleCallback(Handler.java:815) 
        at android.os.Handler.dispatchMessage(Handler.java:104) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5572) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763) 
        Suppressed: java.lang.ClassNotFoundException: javax.imageio.ImageIO
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)

1 个答案:

答案 0 :(得分:0)

Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/imageio/ImageIO;

您正在尝试使用Android中不存在的Java类。由于您的应用已编译,因此这可能来自您正在使用的某些库,而该库并非设计用于Android。