2013-04-29 104 views
0

我知道很多人都问过这个问题,但没有一个人能帮助我,这就是我要的是如何使一个按钮的onclick必须开启照片选择器和上传选择图片到Facebook?上传图片至Facebook

Iam真的卡住了,没有答案可以帮助我。我有一个代码,它发送文本和工作正常。但是现在我也想上传图片,所以我怎么能做到这一点?任何人都可以帮助我?

问候

更新 - >这是我必须张贴

public class TestPost extends Activity{ 
private Facebook mFacebook; 
private CheckBox mFacebookCb; 
private ProgressDialog mProgress; 

private Handler mRunOnUi = new Handler(); 

private static final String APP_ID = "app id here"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.post); 

    final EditText reviewEdit = (EditText) findViewById(R.id.revieew); 
    mFacebookCb    = (CheckBox) findViewById(R.id.cb_facebook); 

    mProgress = new ProgressDialog(this); 

    mFacebook = new Facebook(APP_ID); 

    SessionStore.restore(mFacebook, this); 

    if (mFacebook.isSessionValid()) { 
     mFacebookCb.setChecked(true); 

     String name = SessionStore.getName(this); 
     name  = (name.equals("")) ? "Unknown" : name; 

     mFacebookCb.setText(" Facebook (" + name + ")"); 
    } 

    ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String review = reviewEdit.getText().toString(); 

      if (review.equals("")) return; 

      if (mFacebookCb.isChecked()) postToFacebook(review); 
     } 
    }); 
    private void postToFacebook(String review) { 
    mProgress.setMessage("Posting ..."); 
    mProgress.show(); 

    AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook); 

    Bundle params = new Bundle(); 

    params.putString("message", review); 
    params.putString("name", "Dexter"); 
    params.putString("caption", "londatiga.net"); 
    params.putString("link", "http://www.londatiga.net"); 
    params.putString("description", "Dexter, seven years old dachshund who loves to catch cats, eat carrot and krupuk"); 
    params.putString("picture", "http://twitpic.com/show/thumb/6hqd44"); 

    mAsyncFbRunner.request("me/feed", params, "POST", new WallPostListener()); 
} 

private final class WallPostListener extends BaseRequestListener { 
    public void onComplete(final String response) { 
     mRunOnUi.post(new Runnable() { 
      @Override 
      public void run() { 
       mProgress.cancel(); 

       Toast.makeText(TestPost.this, "Posted to Facebook", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

} } 

更新2 - > logcat的和冲击片雷管

04-30 12:45:02.471: W/Bundle(29461): Key method expected byte[] but value was a java.lang.String. The default value <null> was returned. 
    04-30 12:45:02.476: W/Bundle(29461): Attempt to cast generated internal exception: 
    04-30 12:45:02.476: W/Bundle(29461): java.lang.ClassCastException: java.lang.String 
    04-30 12:45:02.476: W/Bundle(29461): at android.os.Bundle.getByteArray(Bundle.java:1305) 
    04-30 12:45:02.476: W/Bundle(29461): at com.facebook.android.Util.openUrl(Util.java:155) 
    04-30 12:45:02.476: W/Bundle(29461): at com.facebook.android.Facebook.request(Facebook.java:559) 
    04-30 12:45:02.476: W/Bundle(29461): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208) 
    04-30 12:45:02.476: W/Bundle(29461): Key format expected byte[] but value was a java.lang.String. The default value <null> was returned. 
    04-30 12:45:02.476: W/Bundle(29461): Attempt to cast generated internal exception: 
    04-30 12:45:02.476: W/Bundle(29461): java.lang.ClassCastException: java.lang.String 
    04-30 12:45:02.476: W/Bundle(29461): at android.os.Bundle.getByteArray(Bundle.java:1305) 
    04-30 12:45:02.476: W/Bundle(29461): at com.facebook.android.Util.openUrl(Util.java:155) 
    04-30 12:45:02.476: W/Bundle(29461): at com.facebook.android.Facebook.request(Facebook.java:559) 
    04-30 12:45:02.476: W/Bundle(29461): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208) 
    04-30 12:45:02.476: W/dalvikvm(29461): threadid=10: thread exiting with uncaught exception (group=0x4001e578) 
    04-30 12:45:02.476: E/AndroidRuntime(29461): FATAL EXCEPTION: Thread-11 
    04-30 12:45:02.476: E/AndroidRuntime(29461): java.lang.NullPointerException 
    04-30 12:45:02.476: E/AndroidRuntime(29461): at java.net.URLDecoder.decode(URLDecoder.java:104) 
    04-30 12:45:02.476: E/AndroidRuntime(29461): at java.net.URLDecoder.decode(URLDecoder.java:48) 
    04-30 12:45:02.476: E/AndroidRuntime(29461): at com.facebook.android.Util.openUrl(Util.java:167) 
    04-30 12:45:02.476: E/AndroidRuntime(29461): at com.facebook.android.Facebook.request(Facebook.java:559) 
    04-30 12:45:02.476: E/AndroidRuntime(29461): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208) 

错误* 这是通话*

((Button) findViewById(R.id.upload)).setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try{ 
      selectImage(); 
        } 
      catch (Exception e) { 
        Toast toast = Toast.makeText(TestPost.this, "Unable to retrieve the selected image.", Toast.LENGTH_LONG); 
        toast.show(); 
       } finally { 
       } 
      } 
    }); 
    } 

这里// TODO:上传到Facebook相册

private void uploadImageBytes(byte[] bytes) { 
    // TODO: upload to Facebook albums 

      Bundle params = new Bundle(); 
      params.putString(Facebook.TOKEN, mFacebook.getAccessToken()); 
      params.putString("method", "photos.upload"); 
      params.putByteArray("picture", bytes); 

      AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook); 
      mAsyncRunner.request(null, params, "POST", new WallPostListener()); 
} 

解决方案 THX与回答问题#vorrtex讲座敬爱他解释我的问题与他的代码,我把它练到工作。这里是给别人完整的代码,如果他们将需要在未来

一个按钮调用方法

((Button) findViewById(R.id.upload)).setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try{ 
      selectImage(); 
        } 
      catch (Exception e) { 
        Toast toast = Toast.makeText(TestPost.this, "Unable to retrieve the selected image.", Toast.LENGTH_LONG); 
        toast.show(); 
       } finally { 
       } 
      } 
    }); 

而且这里的方法和流码。该代码是从回答问题

private void selectImage() { 

    Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
    i.setType("image/*"); 
    this.startActivityForResult(i, REQUEST_CODE_GALLERY); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     switch (requestCode) { 
      case REQUEST_CODE_GALLERY: 

       Uri selectedImage = data.getData(); 
       String filePath = null; 

       String[] columns = { MediaColumns.DATA }; 
       Cursor cursor = this.getContentResolver().query(selectedImage, columns, null, null, null); 
       if (cursor != null) { 
        cursor.moveToFirst(); 

        int columnIndex = cursor.getColumnIndex(columns[0]); 
        filePath = cursor.getString(columnIndex); 

        if (!cursor.isClosed()) { 
         cursor.close(); 
        } 
       } else { 
        filePath = selectedImage.getPath(); 
       } 

       if (filePath != null) { 
        this.uploadImagePath(filePath); 
       } else { 
        Toast toast = Toast.makeText(this, "Unable to retrieve the selected image.", Toast.LENGTH_LONG); 
        toast.show(); 
       } 

       break; 
     } 
    } 
} 

private void uploadImagePath(String filePath) { 
    FileInputStream fileStream = null; 
    try { 
     fileStream = new FileInputStream(new File(filePath)); 
     byte[] bytes = convertStreamToBytes(fileStream); 
     this.uploadImageBytes(bytes); 
     placering = filePath; 
    } catch (Exception e) { 
     Toast toast = Toast.makeText(this, "Unable to retrieve the selected image.", Toast.LENGTH_LONG); 
     toast.show(); 
    } finally { 
     closeStream(fileStream); 
    } 
} 

private void uploadImageBytes(byte[] bytes) { 
    // TODO: upload to Facebook albums 

    Bundle params = new Bundle(); 
    params.putString(Facebook.TOKEN, mFacebook.getAccessToken()); 
    params.putString("method", "photos.upload"); 
    params.putByteArray("picture", bytes); 

    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook); 
    mAsyncRunner.request(null, params, "POST", new WallPostListener()); 
} 


public static byte[] convertStreamToBytes(InputStream stream) throws IOException { 
    if (stream == null) { 
     return null; 
    } 

    ByteArrayOutputStream output = new ByteArrayOutputStream(); 

    copyStream(stream, output); 

    return output.toByteArray(); 
} 

public static void copyStream(InputStream from, OutputStream to) throws IOException { 
    byte data[] = new byte[8192]; 
    int count; 

    while ((count = from.read(data)) != -1) { 
     to.write(data, 0, count); 
    } 

    from.close(); 
} 

public static void closeStream(Closeable stream) { 
    try { 
     if (stream != null) { 
      stream.close(); 
     } 
    } catch (Exception e) { 

    } 
} 
+0

我已经更新的问题,我想在这里跟随一些答案的计算器,但他们大多是像给定的路径,但我希望用户选择图像张贴 – Tirolel 2013-04-29 17:23:55

回答

1

以下解答如何启动照片选择器:How to pick an image from gallery (SD Card) for my app?

那么你应该选择的图像上传到Facebook相册(/me/photos)。你必须这样做,因为me/feed只接受URL字符串,而不是二进制图像,所以你上传图像,以便你有一个URL字符串。

这是一个答案如何做到这一点:Android - Upload photo to Facebook with Facebook Android SDK。您应该从库中选择的文件路径替换'/sdcard/viewitems.png'。

然后你就可以张贴的链接上传的图片:

// the onComplete method from the previous link 
final String src = json.getString("src"); 

// ... create params ... 
params.putString("picture", src); 

mAsyncFbRunner.request("me/feed", params, "POST", new WallPostListener()); 

编辑: 顺便说一句,从第一个链接的答案是不完整的,都不好,所以这里是全代码如何从库中选择图像并将其转换为字节:

private void selectImage() { 
    Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
    i.setType("image/*"); 
    this.startActivityForResult(i, Constants.REQUEST_CODE_GALLERY); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     switch (requestCode) { 
      case Constants.REQUEST_CODE_GALLERY: 

       Uri selectedImage = data.getData(); 
       String filePath = null; 

       String[] columns = { MediaColumns.DATA }; 
       Cursor cursor = this.getContentResolver().query(selectedImage, columns, null, null, null); 
       if (cursor != null) { 
        cursor.moveToFirst(); 

        int columnIndex = cursor.getColumnIndex(columns[0]); 
        filePath = cursor.getString(columnIndex); 

        if (!cursor.isClosed()) { 
         cursor.close(); 
        } 
       } else { 
        filePath = selectedImage.getPath(); 
       } 

       if (filePath != null) { 
        this.uploadImagePath(filePath); 
       } else { 
        Toast toast = Toast.makeText(this, "Unable to retrieve the selected image.", Toast.LENGTH_LONG); 
        toast.show(); 
       } 

       break; 
     } 
    } 
} 

private void uploadImagePath(String filePath) { 
    FileInputStream fileStream = null; 
    try { 
     fileStream = new FileInputStream(new File(filePath)); 
     byte[] bytes = convertStreamToBytes(fileStream); 
     this.uploadImageBytes(bytes); 
    } catch (Exception e) { 
     Toast toast = Toast.makeText(this, "Unable to retrieve the selected image.", Toast.LENGTH_LONG); 
     toast.show(); 
    } finally { 
     closeStream(fileStream); 
    } 
} 

private void uploadImageBytes(byte[] bytes) { 
    // TODO: upload to Facebook albums 
} 

public static byte[] convertStreamToBytes(InputStream stream) throws IOException { 
    if (stream == null) { 
     return null; 
    } 

    ByteArrayOutputStream output = new ByteArrayOutputStream(); 

    copyStream(stream, output); 

    return output.toByteArray(); 
} 

public static void copyStream(InputStream from, OutputStream to) throws IOException { 
    byte data[] = new byte[8192]; 
    int count; 

    while ((count = from.read(data)) != -1) { 
     to.write(data, 0, count); 
    } 

    from.close(); 
} 

public static void closeStream(Closeable stream) { 
    try { 
     if (stream != null) { 
      stream.close(); 
     } 
    } catch (Exception e) { 
     Log.e(TAG, e); 
    } 
} 
+0

您提供的链接是正确的,但是将bytearray放入参数中而不仅仅是一个路径。 – 2013-04-29 18:17:30

+0

@Pork第二个链接的答案使用了类似于'BitmapFactory.decodeFile'和'data = baos.toByteArray()'的方法,所以路径被转换成一个随后使用的字节数组。 – vorrtex 2013-04-29 18:24:21

+0

我同意。应该使用ByteArray而不是String。 – 2013-04-29 18:42:26