2013-05-15 43 views

回答

0

首先你需要知道的圖像條路徑,然後使用Facebook SDK上傳圖片的輪廓

https://developers.facebook.com/android/

在SDK

存在上傳一個例子的圖像

+0

我該如何得到路徑? – James

+0

@james你需要從圖庫中獲取圖片嗎?或者你需要從OT的情況下,你知道文件名 – Raghunandan

+0

SD卡獲取圖像,我相信這樣是 – James

3

要獲得從畫廊

String filepath; // filepath contains path of the file 

public void getImage() 
{ 
    // To open up a gallery browser 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),1); 
    // To handle when an image is selected from the browser, add the following to your Activity 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 1) { 
      // currImageURI is the global variable I'm using to hold the content:// URI of the image 
      Uri currImageURI = data.getData(); 

      File file = new File(getRealPathFromURI(currImageURI)); 

      if (file.exists()) 
      { 
       filepath=file.getAbsolutePath(); 
      } 
      else 
      { 
       System.out.println("File Not Found"); 
      } 
     } 
    } 
} 

public String getRealPathFromURI(Uri contentUri) 
{ 
    // can post image 
    String [] proj={MediaStore.Images.Media.DATA}; 
    Cursor cursor = managedQuery(contentUri, 
     proj, // Which columns to return 
     null, // WHERE clause; which rows to return (all rows) 
     null, // WHERE clause selection arguments (none) 
     null); // Order-by clause (ascending by name) 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 

文件的路徑,從SD卡

獲取圖像3210
File file = new File(android.os.Environment.getExternalStorageDirectory(),"MyDraw"); 
    if(file.exists()) 
    { 
     File myfile = new File(file.getAbsoluteFile()+File.separator+"name.png"); 
     String path = myfile.getAbsolutePath(); 
    } 

要上傳文件到服務器。我不知道如何將文件上傳到Facebook或Twitter。 一般上傳文件到服務器

public void upload(String filepath) 
    { 
     try 
     { 
     HttpClient httpclient = new DefaultHttpClient(); 
     httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 

     HttpPost httppost = new HttpPost("youe url"); 
     File file = new File(filepath); 
     MultipartEntity mpEntity = new MultipartEntity(); 
     ContentBody cbFile = new FileBody(file, "image/jpeg"); 
     mpEntity.addPart("userfile", cbFile); 
     httppost.setEntity(mpEntity); 

     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity resEntity = response.getEntity(); 
     } 
     catch(Exception e) 
     { 
     e.printStackTrace(); 
     } 
    } 
+0

我需要知道整個路徑和所有內容嗎?或者我只需要知道用戶命名圖片的內容? – James

+0

@詹姆斯你需要文件的路徑上傳圖像到服務器。該路徑將爲您提供文件表單的位置,您可以上傳相同類型的文件,以便從桌面上傳圖片。 – Raghunandan

+0

那麼我如何獲得路徑? – James