我的imageView不起作用

时间:2016-06-24 11:58:47

标签: android camera imageview

我在Android工作室工作,我正在尝试从手机目录中导入照片并将其放在ImageView上。

在我的第一个活动中,我尝试使用相机保存照片,并尝试从照片中保存URI。那是在工作。

在另一项活动中,我试图导入该URI但我的ImageView没有改变。编译器从照片中看到了位置,所以我认为是有效的,但是imageview无法导入照片......

你可以帮帮我吗?

public class PictureConfirmation extends Activity {
    ImageButton use;
    ImageButton retake;
    TextView usetxt;
    TextView retaketxt;
    ImageView pictaken;
    String pic;
    File picfile;
    Matrix matrix;
    String pic2;
    Bitmap bitmap;
    public Bitmap rotatedbitmap;
    ImageButton scan;
    ImageView teeth;
    TextView scantxt;
    public String colorRGB;
    TextView canceltxt;
    Uri ImgUri;
    public float ax;

    public float ay;
    int Rc, Gc, Bc;
    public int checkcamera=0;
    TextView colortxt;

    @Override
    protected void onCreate(Bundle savedInstance) {

        final Typeface bariol_font = Typeface.createFromAsset(getAssets(), "Bariol_Bold.otf");
        //going full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstance);
        setContentView(R.layout.picture_confirmation);
        final String PREF_NAME = "Link";
        String defaultValue = "default string";
        Preferences prefs = Preferences.userNodeForPackage(com.example.newrosoft1.dentafy.MainMenuActivity.class);
        String propertyValue = prefs.get(PREF_NAME, defaultValue); // "a string"
        //pic = new MainMenuActivity().getpath();

        pic2=propertyValue;
        pic=propertyValue;
        Log.d("valoare este %@" ,pic.toString());
        //bitmap =BitmapFactory.decodeFile(pic);
       // Uri imgUri=Uri.parse(pic);
        //.setImageURI(imgUri);


        use = (ImageButton) findViewById(R.id.usebtnid);
        retake = (ImageButton) findViewById(R.id.retakebtnid);
        retaketxt = (TextView) findViewById(R.id.retaketxtid);
        retaketxt.setTypeface(bariol_font);
        usetxt = (TextView) findViewById(R.id.usetextid);
        usetxt.setTypeface(bariol_font);
        pictaken = (ImageView) findViewById(R.id.pictakenid);
        //pictaken.setImageBitmap(bitmap);

        Uri imgUri=Uri.parse(pic);


        pictaken.setImageURI(null);
        pictaken.setImageURI(imgUri);
        try {
            pictaken.setImageDrawable(Drawable.createFromStream(
                    getContentResolver().openInputStream(imgUri),
                    null));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        final AlertDialog.Builder alertdialog = new AlertDialog.Builder(this);
        alertdialog.setTitle("Confirmation");
        alertdialog.setMessage("Are you sure this is the correct picture?\nPlease note that you have a limited number of scans");
        alertdialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                setContentView(R.layout.processing);
                Runnable r = new Runnable() {
                    @Override
                    public void run() {
                        setContentView(R.layout.scan_teeth_finished);
                        TextView done = (TextView) findViewById(R.id.donetxtid);
                        done.setTypeface(bariol_font);
                        colortxt = (TextView) findViewById(R.id.teethcolortxtid);
                        colortxt.setTypeface(bariol_font);

                        teeth = (ImageView) findViewById(R.id.teethimg);

                        //teeth.setImageBitmap(bitmap);
                        Uri imgUri=Uri.parse(pic);
                        teeth.setImageURI(null);
                        teeth.setImageURI(imgUri);
                        try {
                            teeth.setImageDrawable(Drawable.createFromStream(
                                    getContentResolver().openInputStream(imgUri),
                                    null));
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }

                        teeth.setOnTouchListener(new View.OnTouchListener() {
                            @Override
                            public boolean onTouch(View view, MotionEvent event) {


                                float eventX = event.getX();
                                float eventY = event.getY();
                                float[] eventXY = new float[]{eventX, eventY};

                                Matrix invertMatrix = new Matrix();
                                ((ImageView) view).getImageMatrix().invert(invertMatrix);

                                invertMatrix.mapPoints(eventXY);
                                int x = Integer.valueOf((int) eventXY[0]);
                                int y = Integer.valueOf((int) eventXY[1]);


                                Drawable imgDrawable = teeth.getDrawable();
                                bitmap = ((BitmapDrawable) imgDrawable).getBitmap();


                                //Limit x, y range within bitmap
                                if (x < 0) {
                                    x = 0;
                                } else if (x > (bitmap.getWidth() - 1)) {
                                    x = bitmap.getWidth() - 1;
                                    ax = x;
                                }

                                if (y < 0) {
                                    y = 0;
                                } else if (y > (bitmap.getHeight() - 1)) {
                                    y = bitmap.getHeight() - 1;
                                    ay = y;

                                }

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


                                int[] areapixels = new int[bitmap.getWidth() * bitmap.getHeight()];

                                int touchedAreaRGB;

                                try {
                                    bitmap.getPixels(areapixels, 0, bitmap.getWidth(), x, y, 25, 25); //pixel area to scan
                                } catch (ArrayIndexOutOfBoundsException arrayindexoutofbounds) {
                                    Toast.makeText(getApplicationContext(), "index out of bounds exception",
                                            Toast.LENGTH_SHORT).show();

                                } catch (IllegalArgumentException illegalargumentexception) {

                                }


                                int total = 0;
                                for (int i = 0; i < areapixels.length; i++) {
                                    if (areapixels[i] != 0)

                                    {
                                        Rc += (areapixels[i] >> 16) & 0xff;
                                        Gc += (areapixels[i] >> 8) & 0xff;
                                        Bc += (areapixels[i]) & 0xff;


                                        total++;
                                    }
                                }
                                if (Rc != 0 || Gc != 0 || Bc != 0) {
                                    Rc /= total;
                                    Gc /= total;
                                    Bc /= total;
                                }
                                touchedAreaRGB = 0xff000000 | (Rc << 16) | (Gc << 8) | Bc;

                                int avrgColor = (Gc - Bc) * 10 / 2;


                                int yellow = 12229738;
                                int white = 16777215;

                                int value = ((touchedRGB - yellow) * 100) / (white - yellow);
                                int value2 = ((touchedRGB - yellow) * 10) / (white - yellow);

                                Log.d("value: ", "asd100 " + value);
                                Log.d("value: ", "asd10 " + value2);


                                colorRGB = "could not find color code, tap again " + value2;
                                if (value2 < 32)
                                    colorRGB = "Selected tooth color is:A1";
                                switch (value2) {

                                    case -33:
                                        colorRGB = "Selected tooth color is:A1 ";
                                        break;
                                    case -32:
                                        colorRGB = "Selected tooth color is:B1";
                                        break;
                                    case -35:
                                        colorRGB = "Selected tooth color is:A2 ";
                                        break;
                                    case -36:
                                        colorRGB = "Selected tooth color is:A3 ";
                                        break;
                                    case -34:
                                        colorRGB = "Selected tooth color is:B2 ";
                                        break;
                                    case -38:
                                        colorRGB = "Selected tooth color is:D2 ";
                                        break;
                                    case -37:
                                        colorRGB = "Selected tooth color is:D3 ";
                                        break;
                                    case -40:
                                        colorRGB = "Selected tooth color is:D4  ";
                                        break;
                                }
                                colortxt.setText(colorRGB);
                                if (Gc < Bc || Gc < 100) {
                                    // Toast.makeText(getApplicationContext(), "Could not get color code, make sure you tap on tooth", Toast.LENGTH_SHORT).show();
                                }

                                //colorRGB.setText("Selected tooth color is:C1" + value2);

                                Rc = 0;
                                Bc = 0;
                                Gc = 0;
                                total = 0;

                                return true;
                            }
                        });
                        done.setOnClickListener(new View.OnClickListener() {
                            @Override

                            public void onClick(View v) {
                                startActivity(new Intent(PictureConfirmation.this, MainMenuActivity.class));
                            }
                        });
                    }
                };
                Handler h = new Handler();
                h.postDelayed(r, 2000);
            }
        });

        alertdialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //do nothing
            }
        });
        alertdialog.create();

        //pictaken.setImageBitmap(bitmap);
        //Uri imgUri=Uri.parse(pic);
        pictaken.setImageURI(null);
        pictaken.setImageURI(imgUri);

        retake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  startActivity(new Intent(PictureConfirmation.this, CameraMainActivity.class));
            }
        });


        use.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.scan_teeth_start);
                scan = (ImageButton) findViewById(R.id.scanbtnid);
                teeth = (ImageView) findViewById(R.id.teethimageid);
                scantxt = (TextView) findViewById(R.id.startscantxtid);
                scantxt.setTypeface(bariol_font);
                canceltxt = (TextView) findViewById(R.id.canceltxtid);
                canceltxt.setTypeface(bariol_font);

                //teeth.setImageBitmap(bitmap);
                Uri imgUri2=Uri.parse(pic);
                teeth.setImageURI(null);
                teeth.setImageURI(imgUri2);
                canceltxt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(PictureConfirmation.this, MainMenuActivity.class));
                    }
                });
                // matrix.postRotate(270);
                //  bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // rotating bitmap
                scan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertdialog.show();
                    }
                });
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

06-24 15:08:30.868 13184-13184 / com.example.newrosoft1.dentafy D / valoare este%@:file:/// file%3A / storage / emulated / 0 / Android / data / com。 example.newrosoft1.dentafy /文件/图片/ JPEG_20160624_150829_558130986.jpg 06-24 15:08:30.868 13184-13184 / com.example.newrosoft1.dentafy D / valoare 2 este%@:file:///file%3A/storage/emulated/0/Android/data/com.example。 newrosoft1.dentafy /文件/图片/ JPEG_20160624_150829_558130986.jpg 06-24 15:08:30.948 13184-13184 / com.example.newrosoft1.dentafy D / valoare este%@:file:///file%3A/storage/emulated/0/Android/data/com.example.newrosoft1 .dentafy /文件/图片/ JPEG_20160624_150829_558130986.jpg 06-24 15:08:30.948 13184-13184 / com.example.newrosoft1.dentafy D / valoare 2 este%@:file:///file%3A/storage/emulated/0/Android/data/com.example。 newrosoft1.dentafy /文件/图片/ JPEG_20160624_150829_558130986.jpg

和LOGCAT错误: 06-24 15:08:34.712 13184-13184 / com.example.newrosoft1.dentafy E / ViewRootImpl:sendUserActionEvent()mView == null

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/background2"
    android:paddingTop="97dp">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="0dp">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="250dp"
            android:id="@+id/pictakenid"
            android:layout_alignParentBottom="true"
            android:scaleType="centerCrop"
            android:layout_alignParentTop="true" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/usebtnid"
            android:src="@drawable/use"
            android:layout_marginBottom="46dp"
            android:background="@android:color/transparent"
            android:layout_marginRight="15dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_marginStart="52dp" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/retakebtnid"
            android:src="@drawable/retake2"
            android:background="@android:color/transparent"
            android:layout_alignTop="@+id/usebtnid"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="31dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="USE"
            android:id="@+id/usetextid"
            android:textSize="23dp"
            android:textColor="#ffffff"
            android:textAlignment="center"
            android:contextClickable="false"
            android:layout_marginBottom="44dp"
            android:layout_alignBottom="@+id/usebtnid"
            android:layout_alignStart="@+id/usebtnid"
            android:layout_marginStart="37dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RETAKE"
            android:id="@+id/retaketxtid"
            android:textSize="23dp"
            android:textColor="#ffffff"
            android:layout_marginRight="18dp"
            android:layout_alignTop="@+id/usetextid"
            android:layout_alignEnd="@+id/retakebtnid" />

    </RelativeLayout>

</LinearLayout>

这里我创建了文件:

String mCurrentPhotoPath;

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        Uri auxPath=getU(image);
        Preferences prefs = Preferences.userNodeForPackage(com.example.newrosoft1.dentafy.MainMenuActivity.class);

// Preference key name
        final String PREF_NAME = "Link";

// Set the value of the preference
        String newValue = auxPath.toString();
        prefs.put(PREF_NAME, newValue);

// Get the value of the preference;
// default value is returned if the preference does not exist




        return image;
    }

    public Uri getU(File u){
        String x;
        x=getpath();
        File f = new File (x);
        Uri imageUri = Uri.fromFile(f);
        return imageUri;
    }

    public String getpath(){
        return mCurrentPhotoPath;
    }


}