0

在我的 facebook android 应用程序中,我需要将图像从 sd 卡上传到 facebook 中的特定相册。我可以创建一个与 sd 卡文件夹同名的相册。但是在创建专辑之后它会得到一个异常——android.os.NetworkOnMainThreadException。这是我的代码-

private void uploadImage() 
{
    String ExternalStorageDirectoryPath = Environment
              .getExternalStorageDirectory()
              .getAbsolutePath();

    String targetPath = ExternalStorageDirectoryPath + "/Friends/";


    File targetDirector = new File(targetPath);

    File[] files = targetDirector.listFiles();

    for (File file : files)
    {      
        Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
        final byte[] data = stream.toByteArray();

        facebook.authorize(MainActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener()         
        {                   
            @Override                   
            public void onComplete(Bundle values)
            {

                if(verify)
                {
                    myAlbumId = createAlbumName();
                    verify=false;
                }
                uploadImagesToAlbum(data, myAlbumId);

            }  

            @Override   
            public void onFacebookError(FacebookError error)
            {
                Toast.makeText(MainActivity.this, "FaceBook Error", Toast.LENGTH_LONG).show();
            }

            @Override                     
            public void onError(DialogError e) 
            {
                Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG).show();
            }                      

            @Override                     
            public void onCancel() 
            {
                Toast.makeText(MainActivity.this, "Canceled", Toast.LENGTH_LONG).show();
            }          
        }); 
    }

}

private String createAlbumName()
{
    Bundle params = new Bundle();
    params.putString("name", "Friends");
    params.putString("message", "My Test Album Description Here");
    mAsyncRunner.request("me/albums", params, "POST", new SampleUploadListener(), null);
    String response = "";
    String albumId = "";
    try {
        response = facebook.request("https://graph.facebook.com/me/albums",params,"POST");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        albumId=Util.parseJson(response).getString("id");
    } catch (FacebookError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return albumId;
}

private void uploadImagesToAlbum(byte[] byteArray, String albumId)
{
    if ( albumId == null ) {
        albumId = "me";
    }
    Bundle params = new Bundle();
    params.putByteArray("picture", byteArray);
    mAsyncRunner.request(albumId+"/photos", params, "POST", new SampleUploadListener(), null);


}

这是我的日志-

03-24 04:06:27.954: E/AndroidRuntime(1054): 致命异常: main 03-24 04:06:27.954: E/AndroidRuntime(1054): android.os.NetworkOnMainThreadException 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 java.net.InetAddress.lookupHostByName(InetAddress .java:385) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 03-24 04:06:27.954: E/AndroidRuntime(1054 ): 在 java.net.InetAddress.getAllByName(InetAddress.java:214) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 libcore.net.http.HttpConnection.(HttpConnection.java:70) 03 -24 04:06:27.954: E/AndroidRuntime(1054): 在 libcore.net.http.HttpConnection.(HttpConnection.java:50) 03-24 04:06:27.954:E/AndroidRuntime(1054): 在 libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 libcore.net.http.HttpConnectionPool .get(HttpConnectionPool.java:87) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 03-24 04:06:27.954 : E/AndroidRuntime(1054): 在 libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 libcore.net.http.HttpsURLConnectionImpl$ HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 03-24 04: 06:27.954:E/AndroidRuntime(1054):在 libcore.net.http.HttpEngine。sendSocketRequest(HttpEngine.java:290) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 libcore.net.http.HttpsURLConnectionImpl.connect (HttpsURLConnectionImpl.java:165) 03-24 04:06:27.954: E/AndroidRuntime(1054): at com.facebook.android.Util.openUrl(Util.java:197) 03-24 04:06:27.954: E /AndroidRuntime(1054): 在 com.facebook.android.Facebook.requestImpl(Facebook.java:806) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 com.facebook.android.Facebook.request( Facebook.java:794) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 com.example.androidfacebookpractice.MainActivity。createAlbumName(MainActivity.java:340) 03-24 04:06:27.954: E/AndroidRuntime(1054): at com.example.androidfacebookpractice.MainActivity.access$4(MainActivity.java:331) 03-24 04:06:27.954 : E/AndroidRuntime(1054): 在 com.example.androidfacebookpractice.MainActivity$9.onComplete(MainActivity.java:290) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 com.facebook.android.Facebook .onSessionCallback(Facebook.java:345) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 com.facebook.android.Facebook.access$11(Facebook.java:326) 03-24 04:06: 27.954: E/AndroidRuntime(1054): 在 com.facebook.android.Facebook$1.call(Facebook.java:304) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 com.facebook.Session$3 $1.run(Session.java:1179) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 android.os.Handler.handleCallback(Handler.java:725) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 android.os.Handler.dispatchMessage(Handler.java:92) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在android.os.Looper.loop(Looper.java:137) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 android.app.ActivityThread.main(ActivityThread.java:5039) 03-24 04: 06:27.954: E/AndroidRuntime(1054): 在 java.lang.reflect.Method.invokeNative(Native Method) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 java.lang.reflect.Method。调用(Method.java:511)03-24 04:06:27.954:E/AndroidRuntime(1054):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)03-24 04: 06:27.954: E/AndroidRuntime(1054): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-24 04:06:27.954: E/AndroidRuntime(1054): 在 dalvik.system .NativeStart。主要(本机方法)

如何将图片上传到相册?谢谢

4

2 回答 2

0

当应用程序尝试在其主线程上执行网络操作时,将引发此异常。我使用新线程解决了这个问题

Thread thread = new Thread()
{
    @Override
    public void run() {
        try {
            //Your code goes here
            }                               
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

thread.start();
于 2013-03-24T04:48:05.060 回答
0

您可以发布您的日志,以便所有人都可以帮助您..

它的连接错误不是图像上传错误。

你不能直接连接把你所有的网络访问放在一个线程或处理程序或 asynctask 上。

于 2013-03-24T04:31:07.897 回答