我正在尝试在我的应用中打开PDF文件。
我是通过在儿童活动中使用此代码来实现的:
Intent addPDFIntent = new Intent();
addPDFIntent.setType("*/*");
addPDFIntent.setAction(Intent.ACTION_GET_CONTENT);
addPDFIntent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(addPDFIntent, REQUEST_CODE);
这样可以让我转到我的文件浏览器并选择一个文件。但是,一旦我选择该文件,该程序将转到我的MainActivity(父)OnActivityResult,其resultCode为0,requestCode为0,data == null。
我期望它做什么(它没有做)是返回调用intent的onActivityResult()方法的活动。我已经实现了(除了Log之外没有其他正文告诉我它是否已输入) - 但它从未进入此方法。它返回我的MainActivity而没有数据。
有人可以帮我理解为什么会这样吗?
EDIT2:此外,第二个活动是以StartActivityForResult开始的,但我从来没有在addPDF进程中调用finish()。
编辑:根据要求,这是令人讨厌的课程。
public class PasteActivity extends Activity {
public static final String KEY_PASTE = "paste";
public static final String KEY_TEXT = "text";
public static final String TAG = "SPREE";
static final int REQUEST_CODE = 2;
private EditText mPasteField;
private Button mDoneButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paste);
Log.i("SPREE", "onCreated");
mPasteField = (EditText) findViewById(R.id.pasteField);
mDoneButton = (Button) findViewById(R.id.doneButton);
textShared();
final Intent returnToMain = new Intent(this, MainActivity.class);
returnToMain.putExtra(KEY_PASTE, mPasteField.getText().toString()
.trim());
returnToMain.setAction(Intent.ACTION_DEFAULT);
mDoneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = getIntent();
if (Intent.ACTION_SEND.equals(intent.getAction())) {
startActivity(returnToMain);
} else {
Intent data = new Intent();
data.putExtra(KEY_PASTE, mPasteField.getText().toString()
.trim());
setResult(RESULT_OK, data);
finish();
}
}
});
Bundle received = getIntent().getExtras();
String receievedString = received.getString(MainActivity.KEY_TO_READ);
mPasteField.setText(receievedString);
if (savedInstanceState != null) {
// maintains typed text
mPasteField.setText(savedInstanceState.getString(KEY_TEXT));
}
}
void textShared() {
Log.i("SREE", "textShared()");
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent);
}
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null)
mPasteField.setText(sharedText);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState
.putString(KEY_TEXT, mPasteField.getText().toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.paste, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.clearText:
mPasteField.setText("");
return true;
case R.id.pasteText:
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (clipboard.hasPrimaryClip()) {
@SuppressWarnings("deprecation")
String pasteText = clipboard.getText().toString();
mPasteField.setText(pasteText);
} else
Toast.makeText(this, "Nothing to paste.", Toast.LENGTH_SHORT)
.show();
return true;
case R.id.addPDF:
Intent addPDFIntent = new Intent();
addPDFIntent.setType("*/*");
addPDFIntent.setAction(Intent.ACTION_GET_CONTENT);
addPDFIntent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(addPDFIntent, REQUEST_CODE);
return true;
default:
return true;
}
}
@Override
protected void onStart() {
super.onStart();
textShared();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "WORKS!");
}
答案 0 :(得分:0)
像那样使用
startActivityForResult(addPDFIntent, 1);
完成数据选择功能后,使用返回意图方法
Intent resultIntent = new Intent();
resultIntent.putExtra(PrevGross,your file);
setResult(Activity.RESULT_OK, resultIntent);
以下代码在主类中使用..
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == 1)
{
prevgross = data.getExtras().getstring(your content);
}}}
}我希望此代码可以帮助您的应用。