Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String saveBitmapToImageFile(
- Bitmap pBitmap,
- String pFilename
- ){
- try{
- FileOutputStream fos = new FileOutputStream(
- pFilename
- );
- if (fos!=null){
- /*
- Write a compressed version of the bitmap to the specified outputstream.
- If this returns true,
- the bitmap can be reconstructed by passing a corresponding inputstream to BitmapFactory.decodeStream().
- Note: not all Formats support all bitmap configs directly,
- so it is possible that the returned bitmap from BitmapFactory could be in a different bitdepth,
- and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque pixels).
- */
- Boolean bAbleToRepresentBitmapAsRequested = pBitmap.compress(
- Bitmap.CompressFormat.PNG,
- 100,
- fos
- );
- if (bAbleToRepresentBitmapAsRequested){
- return pFilename;
- }
- }//if
- }//try
- //if an Exception does occur it is probably "FileNotFoundException"
- catch (Exception e){
- //probably: java.io.FileNotFoundException: test.png (Read-only file system)
- Log.e("BOINK!", e.toString());
- }//catch
- return ""; //could not save file
- }//saveBitmapToImageFile
- //---
- <?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">
- <ImageView
- app:layout_constraintTop_toTopOf="parent"
- android:id="@+id/idIvCapturedImage"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <!-- Text captured: -->
- <TextView
- android:gravity="center_horizontal"
- android:text="@string/strTvAbout"
- android:id="@+id/idTvAbout"
- app:layout_constraintTop_toBottomOf="@id/idIvCapturedImage"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"></TextView>
- <!-- the exact text that was shared from an external app will
- show here -->
- <TextView
- android:id="@+id/idTvCapturedText"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toBottomOf="@id/idTvAbout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <ListView
- android:id="@+id/idLvRecords"
- app:layout_constraintTop_toBottomOf="@id/idTvCapturedText"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"></ListView>
- </androidx.constraintlayout.widget.ConstraintLayout>
- //
- package com.joythis.android.textcapturer;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- 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;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Map;
- public class MainActivity extends AppCompatActivity {
- Context mContext;
- TextView mTvAbout, mTvCapturedText;
- ImageView mIvCapturedImage;
- ArrayList<String> mAlRecords;
- ArrayAdapter<String> mAd;
- ListView mLvRecords;
- public final static String MY_TSV_DB = "SHARES.DB";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }//onCreate
- boolean qualityControl(
- Object[] pCheckThese
- ){
- boolean bRet = true;
- for (Object o : pCheckThese){
- bRet = bRet && (o!=null);
- }//for
- return bRet;
- }//qualityControl
- Map<Object, Boolean> altQualityControl(
- Object[] pCheckThese
- ){
- Map<Object, Boolean> aRet= new HashMap<>();
- for (Object o : pCheckThese){
- boolean bOk = o!=null;
- aRet.put(o, bOk);
- }//for
- return aRet;
- }//altQualityControl
- void init(){
- mContext = this;
- mTvAbout = findViewById(R.id.idTvAbout);
- mTvCapturedText = findViewById(R.id.idTvCapturedText);
- mIvCapturedImage = findViewById(R.id.idIvCapturedImage);
- mLvRecords = findViewById(R.id.idLvRecords);
- mAlRecords = new ArrayList<>();
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mAlRecords //memory address for the data
- );
- mLvRecords.setAdapter(mAd);
- Object[] aRelevant = {mTvAbout, mTvCapturedText};
- boolean bCanProceed = qualityControl(aRelevant);
- if (!bCanProceed){
- Toast t = Toast.makeText(
- this,
- getString(R.string.strThereIsNullObject),
- Toast.LENGTH_LONG
- );
- return;
- }//if
- mTvAbout.setText(
- whereIsThePrivateInternalStorage()
- +"\nText Captured:"
- );
- /*
- TODO: avoid reprocessing the Intent on reexecutions of onCreate
- */
- checkIfCalledForSharingAndDisplayingSharedData();
- refresh();
- }//init
- void checkIfCalledForSharingAndDisplayingSharedData(){
- Intent intentHowWasICalled = getIntent();
- if (intentHowWasICalled!=null){
- String strAction =
- intentHowWasICalled.getAction();
- //share
- boolean bIWantToAnswer =
- strAction.equals(Intent.ACTION_SEND);
- boolean bIWantToAnswer2 =
- strAction.compareTo(Intent.ACTION_SEND)==0;
- boolean bIWantToAnswer3 =
- strAction.compareToIgnoreCase(Intent.ACTION_SEND)==0;
- if (bIWantToAnswer){
- //it was a share!
- String strType =
- intentHowWasICalled.getType();
- Toast.makeText(
- mContext,
- strType,
- Toast.LENGTH_LONG
- ).show(); //"image/png" "image/jpeg"
- boolean bIsText =
- strType.startsWith("text/");
- boolean bIsImage =
- strType.startsWith("image/");
- if (bIsText){
- String strTextThatWasShared =
- intentHowWasICalled.getStringExtra(
- Intent.EXTRA_TEXT
- );
- insert(strTextThatWasShared); //record the received text
- refresh(); //shows the text
- mTvCapturedText.setVisibility(View.GONE); //hides the TextView
- //enough for principle demonstration
- mTvCapturedText.setText(
- strTextThatWasShared
- );
- }//if text was shared
- else
- if (bIsImage){
- //TODO?
- }
- }//if the Activity was called from an ACTION_SEND Intent (e.g. Share from another app)
- }//if got an Intent object with all the necessary data
- }//checkIfCalledForSharingAndDisplaySharedText
- void refresh(){
- /*
- String strNewContent = readFile(MY_TSV_DB);
- mTvCapturedText.setText(strNewContent);
- */
- //mAlRecords = new ArrayList<>(); //NO GO! would break the Adapter
- mAlRecords.clear();
- //mAlRecords.addAll(readFile(MY_TSV_DB)); //original order
- ArrayList<String> alTemp = readFile(MY_TSV_DB);
- for (int idx=0; idx<alTemp.size(); idx++){
- String strContent = alTemp.get(idx);
- mAlRecords.add(0, strContent);
- }//for
- mAd.notifyDataSetChanged();
- }//refresh
- /*String*/ArrayList<String> readFile(
- String pFileName
- ){
- String strContent =
- readFromPrivateInternalStorage(pFileName);
- /*
- somewhat useless while the app is not rewritten using
- a true "Record" class data type
- and a collection of Records is kept (for example in ArrayList<Record>)
- */
- ArrayList<String> aRet = new ArrayList<>();
- String[] aRecords =
- strContent.split("\n");
- for(String record : aRecords){
- String strAfterReplacement =
- record.replace(
- "<br>", "\n"
- );
- aRet.add(strAfterReplacement);
- }//for
- return aRet;
- //return strContent;
- }//readFile
- /*
- will record with a TSV structure of our imagination
- the data in a file, in the private internal storage of the app
- <when> \t <text> \n
- */
- void insert(String pStrWhatToInsert){
- Calendar cal = Calendar.getInstance();
- String strYMD = String.format(
- "%d-%d-%d",
- cal.get(Calendar.YEAR),
- (cal.get(Calendar.MONTH)+1),
- cal.get(Calendar.DAY_OF_MONTH)
- );
- //replace the TSV reserved-symbols for innocuos alternatives
- // \n -> "<br>" ; \t -> " "
- String strAfterReplacement =
- pStrWhatToInsert.replace(
- "\n",
- "<br>"
- );
- strAfterReplacement =
- pStrWhatToInsert.replace(
- "\t",
- " "
- );
- String strRecord = String.format(
- "%s\t%s\n",
- strYMD,
- strAfterReplacement
- );
- Boolean bOK =
- writeToPrivateInternalStorage(
- MY_TSV_DB,
- //pStrWhatToInsert+"\n",
- strRecord,
- MODE_APPEND
- );
- if (bOK){
- Toast t = Toast.makeText(
- mContext,
- "New record added to TSV DB",
- Toast.LENGTH_LONG
- );
- t.show();
- }//if
- }//insert
- /*
- to read and write in the Private Internal Storage
- we need tools for dumping and recovering text from
- that exclusive non-volatile file-system area
- */
- String whereIsThePrivateInternalStorage(){
- File pis = this.getFilesDir();
- return pis.getAbsolutePath();
- }//whereIsThePrivateInternalStorage
- Boolean writeToPrivateInternalStorage(
- String pFileName,
- String pFileContent,
- int pWriteMode //PRIVATE (destructive) / APPEND (cumulative)
- ){
- try {
- FileOutputStream fos = openFileOutput(
- pFileName,
- //MODE_APPEND
- pWriteMode
- );
- if (fos!=null){
- OutputStreamWriter osw = new OutputStreamWriter(
- fos,
- StandardCharsets.UTF_8
- );
- if (osw!=null){
- osw.write(pFileContent);
- osw.close();
- fos.close();
- return true;
- }//if
- }//if
- }//try
- catch (Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- return false;
- }//writeToPrivateInternalStorage
- String readFromPrivateInternalStorage(
- String pFileName
- ){
- try{
- FileInputStream fis = this.openFileInput(pFileName);
- if (fis!=null){
- InputStreamReader isr = new InputStreamReader(
- fis,
- StandardCharsets.UTF_8
- );
- if (isr!=null){
- int i; char c; 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 "";
- }//readFromPrivateInternalStorage
- }//MainActivity
Add Comment
Please, Sign In to add comment