我的相机意图不能正常工作

时间:2015-09-16 10:32:04

标签: android

以下是我的代码,当我点击按钮时它将打开相机,只有一个图像将存储在位置但不显示在imageview。 我的logcat将是。请解决我的问题

btn=(Button)findViewById(R.id.camera);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int count=0;
            do {
                Log.v(TAG, "Before Intent");
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                Log.v(TAG, "Intent Created");
                fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
                intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);// set the image file name
                startActivityForResult(intent, count);
                Log.v(TAG, ""+count);
                count++;
            }while(count!=3);
        }
    });

 private static Uri getOutputMediaFileUri(int type){
    Log.v(TAG, "Creating URI Stuff....");
    return Uri.fromFile(getOutputMediaFile(type));
}

private static File getOutputMediaFile(int type){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),TAG);
    Log.v(TAG,"Directory creation");

    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.v(TAG, "failed to create directory");
            return null;
        }
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE)
    {

        File imgFile = new File("\\Phone\\Pictures\\myAppSurun/IMG_one.jpg");
        File imgFile1=new File("\\Phone\\Pictures\\myAppSurun/IMG_two.jpg");
        File imgFile2=new File("\\Phone\\Pictures\\myAppSurun/IMG_three.jpg");
        if (!imgFile.exists()) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_one" + ".jpg");
            Log.v(TAG, "IMG_ONE created...");
        }
        else if(imgFile1.exists())
        {
            mediaFile=new File(mediaStorageDir.getPath()+File.separator+"IMG_three"+".jpg");
            Log.v(TAG, "IMG_THREE created...");
        }
        else if(imgFile.exists() && !imgFile1.exists())
        {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_two" + ".jpg");
            Log.v(TAG, "IMG_TWO created...");
        }
        else
        {
            mediaFile=new File(mediaStorageDir.getPath()+ File.separator+"IMG_one"+".jpg");
            Log.v(TAG, "IMG_ONE ELSE  created...");
        }

    }
    else
    {
        return null;
    }
    return mediaFile;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.v(TAG, "Before request...");
    //  int count=1;

    int count = 0;
    Log.v(TAG, "count" + count);
    do {

        if (requestCode == count) {
            Log.v(TAG, "Camera request iff...");
            if (resultCode == RESULT_OK) {
                Log.v(TAG, "Result Ok pre...");
                Log.v(TAG, ""+count);

                try {
                    File imgFile = new File("\\Phone\\Pictures\\myAppSurun/IMG_one.jpg");
                    if (imgFile.exists()) {
                        Log.v(TAG, "IMG_ONE File Exists...");
                        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                        myBitmap = Bitmap.createScaledBitmap(myBitmap, 50, 50, true);
                        iv.setImageBitmap(myBitmap);
                        long filesize = imgFile.length();
                        long sizeinmb = ((filesize / 1024) / 1024);
                        imgsiz1.setText("" + sizeinmb);
                        Log.v(TAG, "count1" + count);

                    }


                    File imgFile1 = new File("\\Phone\\Pictures\\myAppSurun/IMG_two.jpg");
                    if (imgFile1.exists()) {
                        Log.v(TAG, "IMG_TWO File Exists...");
                        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile1.getAbsolutePath());
                        myBitmap = Bitmap.createScaledBitmap(myBitmap, 50, 50, true);
                        iv1.setImageBitmap(myBitmap);
                        long filesize = imgFile1.length();
                        long sizeinmb = ((filesize / 1024) / 1024);
                        imgsiz1.setText("" + sizeinmb);
                        Log.v(TAG, "count2" + count);

                    }


                    File imgFile2 = new File("\\Phone\\Pictures\\myAppSurun/IMG_three.jpg");
                    if (imgFile2.exists()) {
                        Log.v(TAG, "IMG_THREE File Exists...");
                        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile2.getAbsolutePath());
                        myBitmap = Bitmap.createScaledBitmap(myBitmap, 50, 50, true);
                        iv2.setImageBitmap(myBitmap);
                        long filesize = imgFile2.length();
                        long sizeinmb = ((filesize / 1024) / 1024);
                        imgsiz1.setText("" + sizeinmb);
                        Log.v(TAG, "count3" + count);
                    }

                    // List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);


                } catch (Exception e) {
                    Log.v(TAG, "Exception " + e);
                }
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Image save canceled", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Image save failed", Toast.LENGTH_LONG).show();
            }
        }
        count++;
    }while(count>=3);

    }

这是我的主要代码...............

/{{([^}]+)}}/

1 个答案:

答案 0 :(得分:0)

试试File imgFile = new File("/Phone/Pictures/myAppSurun/IMG_one.jpg");。在android文件路径分隔符是&#34; /&#34;不是&#34; \&#34;。或者你可以使用File.pathSeparator