Делитесь и получайте простые данные из файла - PullRequest
0 голосов
/ 01 мая 2020

С помощью этой справки Я хочу восстановить текст внутри файла, долго нажимаю на файл, могу выбрать приложение для использования, поэтому я беру то, которое понимаю, приложение принимает во внимание мои действия, но я не могу восстановить текст из файла. Переменная «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>

Спасибо за ваши ответы.

1 Ответ

0 голосов
/ 03 мая 2020

Эврика, здесь много серфинга - это решение моей проблемы, и я надеюсь, что это поможет другим людям. Я видел много подобных проблем, вот ссылка, которая помогла мне lien

а вот код

    @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);




        }
     }
}
...