如果我有点难以理解,我很抱歉。我是Android新手。我正在尝试制作一个应用程序,在twitter上发布图像和文本作为状态更新。我正在关注这个
http://chintankhetiya.wordpress.com/2013/07/18/sharing-text-image-in-twitter-android-example/
现在,他正在提供图片的网址并将其转换为文件,然后在Twitter上发布。我希望用户能够从图库中选择图像或从相机拍摄照片,然后在功能中设置为文件。我能做什么。?我添加了按钮来选择图像并在imageview中显示。但是如何将其转换为文件并将其传递给函数。
这是类文件:
public class MainActivity extends Activity {
// Replace your KEY here and Run ,
public final String consumer_key = "trWwomp0b09ER2A8H1cQg";
public final String secret_key = "PAC3E3CtcPcTuPl9VpCuzY6eDD8hPZPwp6gRDCviLs";
File casted_image;
String string_img_url = null , string_msg = null;
Button btn, pick;
EditText et;
ImageView iv;
private static int RESULT_LOAD_IMAGE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main);
iv = (ImageView) findViewById(R.id.imageView1);
pick = (Button) findViewById(R.id.button1);
pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onClickTwitt();
}
});
} catch (Exception e) {
// TODO: handle exception
runOnUiThread(new Runnable() {
public void run() {
showToast("View problem");
}
});
}
}
public void Call_My_Blog(View v) {
Intent intent = new Intent(MainActivity.this, My_Blog.class);
startActivity(intent);
}
// Here you can pass the string message & image path which you want to share
// in Twitter.
public void onClickTwitt() {
if (isNetworkAvailable()) {
Twitt_Sharing twitt = new Twitt_Sharing(MainActivity.this, consumer_key, secret_key);
string_img_url = "http://www.bharatbpo.in/bbpo/images/AndroidLogo.jpg";
et = (EditText) findViewById(R.id.editText1);
string_msg = et.getText().toString();
// here we have web url image so we have to make it as file to
// upload
// Now share both message & image to sharing activity
twitt.shareToTwitter(string_msg, casted_image);
} else {
showToast("No Network Connection Available !!!");
}
}
// when user will click on twitte then first that will check that is
// internet exist or not
public boolean isNetworkAvailable() {
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo [ ] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
private void showToast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
protected void onActivityResult(String requestCode, int resultCode, Intent data) {
if (requestCode == "Select Image" && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
casted_image = new File(picturePath);
}
}
现在,我希望用户能够点击图片或从图库中选择图片,而不是此图片的网址。请帮忙。我无法上传图片。这是日志:
09-10 15:26:13.962: W/IInputConnectionWrapper(1260): showStatusIcon on inactive InputConnection
09-10 15:26:18.052: V/log_tag(1260): Pic Upload error403:The request is understood, but it has been refused. An accompanying error message will explain why. This code is used when requests are being denied due to update limits (https://support.twitter.com/articles/15364-about-twitter-limits-update-api-dm-and-following).
09-10 15:26:18.052: V/log_tag(1260): message - Status is a duplicate.
09-10 15:26:18.052: V/log_tag(1260): code - 187
09-10 15:26:18.052: V/log_tag(1260): Relevant discussions can be found on the Internet at:
09-10 15:26:18.052: V/log_tag(1260): http://www.google.co.jp/search?q=2fc5b7cb or
09-10 15:26:18.052: V/log_tag(1260): http://www.google.co.jp/search?q=0e8e77fd
09-10 15:26:18.052: V/log_tag(1260): TwitterException{exceptionCode=[2fc5b7cb-0e8e77fd], statusCode=403, message=Status is a duplicate., code=187, retryAfter=-1, rateLimitStatus=null, version=4.0.2}
09-10 15:26:26.262: I/dalvikvm(1260): Jit: resizing JitTable from 512 to 1024
答案 0 :(得分:1)
通过EXTRA_ALLOW_MULTIPLE
方法在意图上设置Intent.putExtra()
选项:
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
上面的代码应如下所示:
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE );
//intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
注意:EXTRA_ALLOW_MULTIPLE选项仅适用于Android API 18及更高版本。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
casted_image = new File(picturePath);
cursor.close();
// String picturePath contains the path of selected Image
}
}
答案 1 :(得分:0)
您需要启动意图从图库中选择图片。
这是一个有用的链接 http://sudhanshuvinodgupta.blogspot.in/2012/07/using-intentactionpick.html?m=1