我正在开发Android应用程序。在我的应用程序中,我需要将url转换为位图。我在网上搜索,我在stackoverflow中找到了一些代码。我刚刚用过它。但它不起作用。它给了我错误。
这是我的转换方法
public final class CommonHelper{
public static Bitmap ConvertUrlToBitmap(String src)
{
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
因此,当我在活动中使用该方法时,它会给我错误。这是logcat中的错误。
这是错误
05-05 17:41:16.674 10095-10101/? E/jdwp: Failed sending reply to debugger: Broken pipe
05-05 17:41:16.674 10095-10101/? D/dalvikvm: Debugger has detached; object registry had 1 entries
05-05 17:41:16.770 10095-10095/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
05-05 17:41:16.770 10095-10095/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
05-05 17:41:16.770 10095-10095/? W/dalvikvm: VFY: unable to resolve interface method 18254: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
05-05 17:41:16.770 10095-10095/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
05-05 17:41:16.770 10095-10095/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
05-05 17:41:16.770 10095-10095/? W/dalvikvm: VFY: unable to resolve interface method 18258: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
05-05 17:41:16.770 10095-10095/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
05-05 17:41:16.782 10095-10098/? D/dalvikvm: GC_CONCURRENT freed 169K, 12% free 2684K/3020K, paused 2ms+0ms, total 5ms
05-05 17:41:16.786 10095-10095/? I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
05-05 17:41:16.786 10095-10095/? I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
05-05 17:41:16.786 10095-10095/? W/dalvikvm: VFY: unable to resolve virtual method 522: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
05-05 17:41:16.786 10095-10095/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
05-05 17:41:16.790 10095-10095/? I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
05-05 17:41:16.790 10095-10095/? W/dalvikvm: VFY: unable to resolve virtual method 544: Landroid/content/res/TypedArray;.getType (I)I
05-05 17:41:16.790 10095-10095/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
05-05 17:41:16.798 10095-10095/? D/dalvikvm: GC_FOR_ALLOC freed 37K, 10% free 2814K/3120K, paused 3ms, total 3ms
05-05 17:41:16.798 10095-10095/? I/dalvikvm-heap: Grow heap (frag case) to 3.981MB for 1127532-byte allocation
05-05 17:41:16.802 10095-10104/? D/dalvikvm: GC_FOR_ALLOC freed <1K, 8% free 3914K/4224K, paused 3ms, total 3ms
05-05 17:41:16.806 10095-10098/? D/dalvikvm: GC_CONCURRENT freed <1K, 8% free 3914K/4224K, paused 0ms+0ms, total 2ms
05-05 17:41:16.834 10095-10098/? D/dalvikvm: GC_CONCURRENT freed 159K, 8% free 4153K/4480K, paused 2ms+0ms, total 5ms
05-05 17:41:16.846 10095-10095/? I/CURRENT_LANGUAGE: 1
05-05 17:41:16.850 10095-10095/? D/AndroidRuntime: Shutting down VM
05-05 17:41:16.850 10095-10095/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4b60648)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: FATAL EXCEPTION: main
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blog.waiyanhein.mmfashion.mmfashion/com.blog.waiyanhein.mmfashion.mmfashion.MainActivity}: android.os.NetworkOnMainThreadException
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: Caused by: android.os.NetworkOnMainThreadException
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.io.IoBridge.connect(IoBridge.java:112)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at java.net.Socket.connect(Socket.java:842)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at com.blog.waiyanhein.mmfashion.model.CommonHelper.ConvertUrlToBitmap(CommonHelper.java:595)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at com.blog.waiyanhein.mmfashion.mmfashion.MainActivity.addCategoriesToMenu(MainActivity.java:345)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at com.blog.waiyanhein.mmfashion.mmfashion.MainActivity.bindCategoryMenu(MainActivity.java:376)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at com.blog.waiyanhein.mmfashion.mmfashion.MainActivity.onCreate(MainActivity.java:101)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5133)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-05 17:41:16.850 10095-10095/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
那么请问为什么会发生这种情况,我该如何解决?如何将网址转换为位图?
我也试过这种方式
public static Bitmap ConvertUrlToBitmap(String src)
{
try{
URL url = new URL(src);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return image;
}
catch (IOException e)
{
return null;
}
}
答案 0 :(得分:0)
您正尝试在线连接并执行网络任务。所有网络任务必须在单独的线程中完成。
尝试一下:
private Bitmap bitmap;
private class ConvertUrlToBitmap extends AsyncTask<String, Long, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
try {
URL url = new URL(params[0]);
bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return true;
} catch (Exception e) {
Log.e(TAG, e.toString());
return false;
}
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if(aBoolean) {
// download was successful
// if you want to update your UI, make sure u do it on main thread. like this:
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// update UI here
}
});
} else {
// download failed
}
}
}
然后在按钮上单击或当您想要下载它时,只需调用:
new ConvertUrlToBitmap().execute(src);
希望这有帮助!
答案 1 :(得分:0)
您正试图在主线程中运行网络消耗任务(可能会阻止UI被刷新 - 这就是Android抱怨的原因)。
您应该启动一个AsyncTask,您可以使用它来下载图像并以专用的回调方式返回。
答案 2 :(得分:0)
在setContentView(R.layout.activity_your)
;
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
将import语句放到你的java文件中。
import android.os.StrictMode;
答案 3 :(得分:0)
尝试将您的代码放入Thread
。
我认为,您可以使用WebView
来显示网址图片:
webView.loadUrl(urlImage)
答案 4 :(得分:0)
为此目的使用AsyncTask。你可以尝试一下。
class GetImage extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
public Bitmap doInBackground(String... urls) {
Bitmap map = null;
try {
URL url = new URL(urls[0]);
HttpURLConnection connection =(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
map= BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
protected void onPostExecute(Bitmap bMap) {
try {
if (!isCancelled()) {
if (bMap != null) {
//set your image view here.
}
}
}catch (Exception exception){
exception.printStackTrace();
}
}
}