Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <TextView
- android:gravity="center_horizontal"
- app:layout_constraintTop_toTopOf="parent"
- android:id="@+id/idTvAbout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/strTvAbout"/>
- <TextView
- android:gravity="center_horizontal"
- app:layout_constraintTop_toBottomOf="@id/idTvAbout"
- android:id="@+id/idTvCapturedText"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <ImageView
- app:layout_constraintTop_toBottomOf="@id/idTvCapturedText"
- android:id="@+id/idIvCapturedImage"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </androidx.constraintlayout.widget.ConstraintLayout>
- //
- package com.joythis.android.pdm2textcapturer;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.nio.charset.StandardCharsets;
- public class MainActivity extends AppCompatActivity {
- Context mContext;
- TextView mTvAbout, mTvCapturedText;
- ImageView mIvCapturedImage;
- public final static String MY_DB = "MY_SHARED_TEXTS.DB";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }//onCreate
- void init(){
- mContext = this;
- mTvAbout = findViewById(R.id.idTvAbout);
- mTvCapturedText = findViewById (R.id.idTvCapturedText);
- mIvCapturedImage = findViewById (R.id.idIvCapturedImage);
- checkIfCalledByAnotherAppAndReceiveItsSharedData();
- mTvAbout.setText(
- whereIsThePrivateInternalStorage()
- +
- "\n"
- +
- getString(R.string.strTvAbout)
- );
- }//init
- void checkIfCalledByAnotherAppAndReceiveItsSharedData(){
- Intent intentHowWasICalled = getIntent();
- if (intentHowWasICalled!=null){
- String strAction = intentHowWasICalled.getAction();
- boolean bItWasTheUserInterfaceOrAndroidStudio =
- strAction.equals(Intent.ACTION_MAIN);
- boolean bIsItActionSend =
- strAction.equals(Intent.ACTION_SEND); //share!
- //is this a share situation?
- if (bIsItActionSend){
- String strType =
- intentHowWasICalled.getType(); //text/html text/plain
- //curious (serves both situations: text and image)
- Toast.makeText(
- mContext,
- strType,
- Toast.LENGTH_LONG
- ).show();
- boolean bIsItSharedText =
- strType.startsWith("text/");
- boolean bIsItSharedImage =
- strType.startsWith("image/");
- if (bIsItSharedText){
- //receive the shared text
- String strSharedText =
- intentHowWasICalled.getStringExtra(
- Intent.EXTRA_TEXT
- );
- //display the received text in mTvCapturedText
- mTvCapturedText.setText(strSharedText);
- insert(strSharedText);
- }//if
- if (bIsItSharedImage){
- //TODO: how to handle image sharing?
- Uri uriForTheImage =
- intentHowWasICalled.getParcelableExtra(
- Intent.EXTRA_STREAM
- );
- mIvCapturedImage.setImageURI(uriForTheImage);
- }
- }//if
- }//if
- }//checkIfCalledByAnotherAppAndReceiveItsSharedData
- Boolean insert(String pInsertWhat){
- return writeContentToFileInPIS(
- MY_DB,
- pInsertWhat+"\n", //TODO! "when \t what \n"
- MODE_APPEND
- );
- }//Boolean
- //---------------------------------------
- /*
- private internal storage?
- non-volatile memory region in the file system
- exclusive to each app
- */
- String whereIsThePrivateInternalStorage(){
- File pis = this.getFilesDir();
- return pis.getAbsolutePath();
- }//whereIsThePrivateInternalStorage
- Boolean writeContentToFileInPIS(
- String pFileName,
- String pFileContent,
- int pWriteMode //MODE_PRIVATE , MODE_APPEND
- ){
- try{
- FileOutputStream fos =
- this.openFileOutput(
- pFileName,
- pWriteMode
- );
- if (fos!=null){
- //fos.write(pFileContent);
- OutputStreamWriter osw =
- new OutputStreamWriter(
- fos,
- StandardCharsets.UTF_8
- );
- if (osw!=null){
- osw.write(pFileContent);
- osw.close();
- fos.close();
- return true;
- }//if got OutputStreamWrite
- }//if got FileOutputStream
- }//try
- catch (Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- return false;
- }//writeContentToFileInPIS
- String readContentFromFileInPIS (
- String pFileName
- ){
- try {
- FileInputStream fis = this.openFileInput(pFileName);
- if (fis!=null){
- InputStreamReader isr = new InputStreamReader(
- fis,
- StandardCharsets.UTF_8
- );
- if (isr!=null){
- char c; int i; String strAll = "";
- while((i=isr.read())!=-1){
- c = (char)i;
- strAll+=c;
- }//while
- isr.close();
- fis.close();
- return strAll;
- }//if
- }//if
- }//try
- catch (Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- return "";
- }//readContentFromFileInPIS
- }//MainActivity
Add Comment
Please, Sign In to add comment