在我的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):致命异常:主要 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):at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 03-24 04:06:27.954:E / AndroidRuntime(1054):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 03-24 04:06:27.954:E / AndroidRuntime(1054):at java.net.InetAddress.getAllByName(InetAddress.java:214) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpConnection。(HttpConnection.java:70) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpConnection。(HttpConnection.java:50) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 03-24 04:06:27.954:E / AndroidRuntime(1054):at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) 03-24 04:06:27.954:E / AndroidRuntime(1054):at 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):at com.facebook.android.Facebook.requestImpl(Facebook.java:806) 03-24 04:06:27.954:E / AndroidRuntime(1054):at com.facebook.android.Facebook.request(Facebook.java:794) 03-24 04:06:27.954:E / AndroidRuntime(1054):at 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):at com.example.androidfacebookpractice.MainActivity $ 9.onComplete(MainActivity.java:290) 03-24 04:06:27.954:E / AndroidRuntime(1054):at com.facebook.android.Facebook.onSessionCallback(Facebook.java:345) 03-24 04:06:27.954:E / AndroidRuntime(1054):at com.facebook.android.Facebook.access $ 11(Facebook.java:326) 03-24 04:06:27.954:E / AndroidRuntime(1054):at com.facebook.android.Facebook $ 1.call(Facebook.java:304) 03-24 04:06:27.954:E / AndroidRuntime(1054):at 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):at java.lang.reflect.Method.invokeNative(Native Method) 03-24 04:06:27.954:E / AndroidRuntime(1054):at java.lang.reflect.Method.invoke(Method.java:511) 03-24 04:06:27.954:E / AndroidRuntime(1054):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 03-24 04:06:27.954:E / AndroidRuntime(1054):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-24 04:06:27.954:E / AndroidRuntime(1054):at dalvik.system.NativeStart.main(Native Method)
如何将图片上传到相册?感谢
答案 0 :(得分:0)
您可以发布您的日志,以便所有人都可以帮助您..
连接错误而非图片上传错误。
无法直接将所有网络访问权限放在线程或处理程序或asynctask上。
答案 1 :(得分:0)
当应用程序尝试在其主线程上执行网络操作时,抛出此异常。 我使用新的线程
解决了这个问题Thread thread = new Thread()
{
@Override
public void run() {
try {
//Your code goes here
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();