Shared Preferences - Some cell phones error

0

Is there any reason for an application that uses Shared Preferences to work on only a few phones?

Yes, the reason for the particular application to stop working on some phones is Shared Preferences. If I leave the sharedpreferences annotated as an annotation, the application starts normally:

MainActivity class:

package com.aeffy.imagker;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1;

    String imageName = "IMG_COLOR_PICKER_LIVE";
    TextView takePhoto, title, help;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Typeface awesome = Typeface.createFromAsset(getAssets(), "fonts/fontawesome.ttf");
        Typeface viga = Typeface.createFromAsset(getAssets(), "fonts/Viga.ttf");
        Typeface title = Typeface.createFromAsset(getAssets(), "fonts/KaushanScript-Regular.ttf");


        this.takePhoto = (TextView) findViewById(R.id.takePhoto);
        this.title = (TextView) findViewById(R.id.title);
        this.help = (TextView) findViewById(R.id.help);

        this.help.setTypeface(viga);
        this.takePhoto.setTypeface(awesome);
        this.title.setTypeface(title);

        this.takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = getFile();
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            String path = "sdcard/temp_hexify/" + imageName + ".png";
            Intent imageActivity = new Intent(MainActivity.this, ImageActivity.class);
            imageActivity.putExtra("path", path);
            startActivity(imageActivity);
        }
    }

    public File getFile(){
        File folder = new File("sdcard/temp_hexify");
        if (!folder.exists()){
            folder.mkdir();
        }
        File image = new File(folder, imageName + ".png");
        return image;
    }

}

ImageActivity class:

package com.aeffy.imagker;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Map;

public class ImageActivity extends AppCompatActivity {

    ImageView image;
    TextView preview, hex, back, save, historic, trash;
    SharedPreferences settings;
    Spinner spinner;
    ArrayList<String> codes;
    Toast toast = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);

        Typeface awesome = Typeface.createFromAsset(getAssets(), "fonts/fontawesome.ttf");
        Typeface viga = Typeface.createFromAsset(getAssets(), "fonts/Viga.ttf");

        this.image = (ImageView) findViewById(R.id.image);
        this.preview = (TextView) findViewById(R.id.color);
        this.hex = (TextView) findViewById(R.id.hex);
        this.back = (TextView) findViewById(R.id.back);
        this.save = (TextView) findViewById(R.id.save);
        this.spinner = (Spinner) findViewById(R.id.spinner);
        this.historic = (TextView) findViewById(R.id.historic);
        this.trash = (TextView) findViewById(R.id.trash);

        this.preview.setTypeface(awesome);
        this.back.setTypeface(awesome);
        this.trash.setTypeface(awesome);
        this.save.setTypeface(awesome);
        this.historic.setTypeface(awesome);
        this.hex.setTypeface(viga);

        String appname = getResources().getString(R.string.app_name);
        settings = getSharedPreferences(appname, MODE_PRIVATE);

        codes = new ArrayList<String>();

        this.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String currentColorCode = hex.getText().toString();
                String message = getResources().getString(R.string.saved) + " " + currentColorCode;
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(ImageActivity.this, message, Toast.LENGTH_LONG);
                toast.show();
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("CODE" + currentColorCode, currentColorCode);
                editor.commit();
            }
        });

        this.trash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                codes.clear();
                SharedPreferences.Editor editor = settings.edit();
                editor.clear();
                editor.commit();
                String message = getResources().getString(R.string.cleaned);
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(ImageActivity.this, message, Toast.LENGTH_LONG);
                toast.show();
            }
        });

        this.historic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, ?> keys = settings.getAll();
                for (Map.Entry<String, ?> entry : keys.entrySet()) {
                    codes.add(entry.getValue().toString());
                }
                if (codes.size() > 0) {
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            ImageActivity.this, android.R.layout.simple_spinner_dropdown_item, codes);
                    spinner.setAdapter(adapter);
                    spinner.performClick();
                } else {
                    String message = getResources().getString(R.string.empty);
                    if (toast != null) {
                        toast.cancel();
                    }
                    toast = Toast.makeText(ImageActivity.this, message, Toast.LENGTH_LONG);
                    toast.show();
                }
            }
        });

        this.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ImageActivity.this, MainActivity.class));
                finish();
            }
        });

        Intent intent = getIntent();
        String path = intent.getExtras().getString("path");
        this.image.setImageDrawable(Drawable.createFromPath(path));
        this.hex.setText("#ffffff".toUpperCase());

        this.image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float eventX = event.getX();
                float eventY = event.getY();
                float[] eventXY = new float[]{eventX, eventY};

                Matrix invertMatrix = new Matrix();
                image.getImageMatrix().invert(invertMatrix);
                invertMatrix.mapPoints(eventXY);
                int x = Integer.valueOf((int) eventXY[0]);
                int y = Integer.valueOf((int) eventXY[1]);

                try {
                    Drawable drawable = image.getDrawable();
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

                    int intColor = bitmap.getPixel(x, y);

                    String color = String.format("#%06X", (0xFFFFFF & intColor));

                    preview.setTextColor(Color.parseColor(color));
                    hex.setText(color);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
                return true;
            }
        });
    }
}
    
asked by anonymous 20.03.2016 / 06:12

0 answers