从文件共享和接收简单数据
Share and receive simple data from a file
Using this help
我想恢复文件中的文本,我长按文件,我可以选择要使用的应用程序,所以我选择了我意识到的那个,应用程序考虑了我的操作,但我无法从中恢复文本文件。
变量“sharedText”为空。
这是下面的代码。
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textHello = findViewById(R.id.hello);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.d(TAG," action = "+action); // = android.intent.action.SEND
Log.d(TAG," type = "+type); // = text/plain
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
Log.d(TAG, " sharedText = " + sharedText); // = null
if (sharedText != null) {
textHello.setText(sharedText);
} else {
sharedText = "it doesn't work for me";
textHello.setText(sharedText);
}
}
}
}
}
这是宣言
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application>
感谢您的回答。
尤里卡,在这里冲浪很多是我的问题的解决方案,我希望它能帮助其他人,我在这里看到了很多类似的问题是帮助我的 link
这是代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textHello = findViewById(R.id.hello);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.d(TAG, " action = " + action); // = android.intent.action.SEND
Log.d(TAG, " type = " + type); // = text/plain
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
Log.d(TAG, " uri = " + uri);
StringBuilder text = new StringBuilder();
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
String mLine;
while ((mLine = r.readLine()) != null) {
Log.d(TAG, " text = " + mLine);
text.append(mLine);
text.append('\n');
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
textHello.setText(text);
}
}
}
Using this help 我想恢复文件中的文本,我长按文件,我可以选择要使用的应用程序,所以我选择了我意识到的那个,应用程序考虑了我的操作,但我无法从中恢复文本文件。 变量“sharedText”为空。 这是下面的代码。
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textHello = findViewById(R.id.hello);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.d(TAG," action = "+action); // = android.intent.action.SEND
Log.d(TAG," type = "+type); // = text/plain
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
Log.d(TAG, " sharedText = " + sharedText); // = null
if (sharedText != null) {
textHello.setText(sharedText);
} else {
sharedText = "it doesn't work for me";
textHello.setText(sharedText);
}
}
}
}
}
这是宣言
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application>
感谢您的回答。
尤里卡,在这里冲浪很多是我的问题的解决方案,我希望它能帮助其他人,我在这里看到了很多类似的问题是帮助我的 link
这是代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textHello = findViewById(R.id.hello);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.d(TAG, " action = " + action); // = android.intent.action.SEND
Log.d(TAG, " type = " + type); // = text/plain
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
Log.d(TAG, " uri = " + uri);
StringBuilder text = new StringBuilder();
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
String mLine;
while ((mLine = r.readLine()) != null) {
Log.d(TAG, " text = " + mLine);
text.append(mLine);
text.append('\n');
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
textHello.setText(text);
}
}
}