簡體   English   中英

我的相機意圖無法正常工作

[英]My Camera Intent Doesnt Work Properly

以下是我的代碼,當我單擊按鈕時,它將打開相機,並且只有一個圖像會存儲在該位置,但不會顯示在imageview上。 和我的logcat將。請解決我的問題

Before
IntentIntent 
Created
Creating URI Stuff....
Directory creation
IMG_ONE created...
0
Before Intent
 Intent Created
 Creating URI Stuff....
 Directory creation
 IMG_ONE created...
   1
Before Intent
Intent Created
Creating URI Stuff....
Directory creation 
IMG_ONE created...
2   

這是我的主要代碼......

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);

    }

嘗試File imgFile = new File("/Phone/Pictures/myAppSurun/IMG_one.jpg"); 在android中,文件路徑分隔符是“ /”而不是“ \\”。 或者您可以使用File.pathSeparator

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM