2013-03-30 34 views
2

我仍然在使用下面的代碼,其中我已經給出了一個特定的圖像張貼在牆上每次,但現在我想讓用戶打開手機畫廊和選擇一個圖像,然後張貼到Facebook牆允許用戶打開手機花屏,選擇一個圖像,然後發佈到Facebook牆

請寫一些代碼如何打開畫廊,然後選擇一個圖像發佈,也顯示如何顯示選定的圖像在我的工作代碼,就像我仍然使用這條線:

   params.putString("picture", 
             FacebookUtility.HACK_ICON_URL); 

的項目單擊完成代碼:

public void onItemClick(AdapterView<?> adapterView, View view, 
     int position, long id) { 

    try { 
     final long friendId; 
     friendId = jsonArray.getJSONObject(position).getLong("uid"); 
     String name = jsonArray.getJSONObject(position).getString("name"); 
     new AlertDialog.Builder(this) 
       .setTitle(R.string.post_on_wall_title) 
       .setMessage(
         String.format(getString(R.string.post_on_wall), 
           name)) 
       .setPositiveButton(R.string.yes, 
         new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int which) { 


           Bundle params = new Bundle(); 

           params.putString("to", 
             String.valueOf(friendId)); 
           params.putString("caption", 
             getString(R.string.app_name)); 
           params.putString("description", 
             getString(R.string.app_desc)); 
           params.putString("link", 
             "http://www.XXX.com"); 
           params.putString("picture", 
             FacebookUtility.HACK_ICON_URL);       
           params.putString("name", 
             getString(R.string.app_action)); 
           FacebookUtility.facebook 
             .dialog(FriendsList.this, 
               "feed", 
               params, 
               (DialogListener) new PostDialogListener()); 
          } 

         }).setNegativeButton(R.string.no, null).show(); 
    } catch (JSONException e) { 
     showToast("Error: " + e.getMessage()); 
    } 
} 

回答

0
public static Bitmap bmpScale; 
public static byte[] data;  

     builder.setNegativeButton("Gallery", 
      new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        openGallery(SELECT_FILE1); 

       } 
      }); 


       public void openGallery(int SELECT_FILE1) { 

    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(
      Intent.createChooser(intent, "Select file to upload "), 
      SELECT_FILE1); 

    // new UploadMediaAsyncTask().execute(); 
} 



    // .................. 
protected void onActivityResult(int requestCode, int resultCode, 
     Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
    Uri selectedImageUri = null; 
    String filePath = null; 
    switch (requestCode) { 
    case SELECT_FILE1: 
     if (resultCode == Activity.RESULT_OK) { 
      selectedImage = imageReturnedIntent.getData(); 

      if (requestCode == SELECT_FILE1) { 
       selectedPath1 = getPath(selectedImage); 
       // mimagepath.setText(selectedPath1); 
       Toast.makeText(Camera.this, "" + selectedPath1 + "", 500) 
         .show(); 

       if (selectedPath1 != null) { 

        BitmapFactory.Options options = new BitmapFactory.Options(); 

        // Decode bitmap with inSampleSize set 
        options.inJustDecodeBounds = true; 
        // image path `String` where your image is located 
        BitmapFactory.decodeFile(selectedPath1, options); 

        int bitmapWidth = 400; 
        int bitmapHeight = 250; 
        final int height = options.outHeight; 
        final int width = options.outWidth; 
        int inSampleSize = 1; 
        if (height > bitmapHeight || width > bitmapWidth) { 
         if (width > height) { 
          inSampleSize = Math.round((float) height 
            /(float) bitmapHeight); 
         } else { 
          inSampleSize = Math.round((float) width 
            /(float) bitmapWidth); 
         } 
        } 

        options.inJustDecodeBounds = false; 
        options.inSampleSize = inSampleSize; 

        bmpScale = BitmapFactory.decodeFile(
          selectedPath1, options); 

        imgggg.setImageBitmap(bmpScale); 
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

        // CompressFormat set up to JPG, you can change to PNG 
        // or 
        // whatever you 
        // want; 

        bmpScale.compress(CompressFormat.JPEG, 100, bos); 
        bmpScale.compress(CompressFormat.JPEG, 100, bos); 
        // ********************** 
        // 
       //globaly.................................. 
        data = bos.toByteArray(); 



       } 

       // Log.d("setpath ", "setpath " + selectedPath1); 
       // Log.d("setpath ", "setpath " + selectedPath1); 
       // System.out.println("selectedPath1 : " + selectedPath1); 
       // mimagepath.setText(selectedPath1); 

      } 
     } 

     break; 


} 

public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 
相關問題