Twitter upload image using MediaService in android

android
image
twitter4j

#1

I have a button select image from sdcard !

 btn_select.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);

        }

    });

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
        selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        img_status.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }

}

And a button upload image to Twitter wall

  btn_post_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TwitterSession session = Twitter.getSessionManager().getActiveSession();
            getWritedata(session);
        }


    });

public void getWritedata(TwitterSession session) {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());

    mFile = new File(selectedImage.getPath() + File.separator + timeStamp + ".PNG");
    typedFile = new TypedFile("image/jpeg", mFile);
    Log.e("KTEE", typedFile + "");
    status = edt_status.getText().toString();
    TwitterApiClient authClient = TwitterCore.getInstance().getApiClient(session);
    MediaService ms = authClient.getMediaService();
    ms.upload(typedFile, null, null, new Callback<Media>() {
        @Override
        public void success(Result<Media> result) {
            Toast.makeText(getActivity(), "Upload completed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void failure(TwitterException e) {
            e.printStackTrace();
            Toast.makeText(getActivity(), "Upload Error", Toast.LENGTH_SHORT).show();

        }
    });

}

I try it but it is no working.
Please, Can you help me!
Thanks you very much!


#2