我还在使用下面的代码,其中我已经给出了一个特定的图像,每次在墙上发布,但现在我想允许用户打开手机图库并选择图像,然后发布到Facebook墙
请写一些代码如何打开图库,然后选择要发布的图像,并显示如何在我的工作代码中显示所选图像,就像我仍在使用此行:
params.putString("picture",
FacebookUtility.HACK_ICON_URL);
完成ItemClick的代码:
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 :(得分: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);
}