2012-01-28 63 views
1

我知道這個問題已經問了很多次在這個論壇。但我仍然無法得到解決方案。 基本上在我的應用程序,我打電話內置相機意圖,捕獲圖像,並在ImageView中顯示位圖並將其存儲在SD卡中。現在,我在我的文件夾中獲得的圖像尺寸很小,就像縮略圖一樣。從相機捕獲的圖像是不是全尺寸存儲

我的代碼是

 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(Intent.createChooser(cameraIntent, "Select picture"), CAMERA_REQUEST); 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

      try { 
     if (requestCode == CAMERA_REQUEST) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 

      if (photo != null) { 
       imageView.setImageBitmap(photo); 
      } 

      // Image name 

      final ContentResolver cr = getContentResolver(); 
      final String[] p1 = new String[] { MediaStore.Images.ImageColumns._ID, 
        MediaStore.Images.ImageColumns.DATE_TAKEN }; 
      Cursor c1 = cr.query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, 
        null, p1[1] + " DESC"); 
      if (c1.moveToFirst()) { 
       String uristringpic = "content://media/external/images/media/" + c1.getInt(0); 
       Uri newuri = Uri.parse(uristringpic); 

       String snapName = getRealPathFromURI(newuri); 

       Uri u = Uri.parse(snapName); 

       File f = new File("" + u); 
       String fileName = f.getName(); 

       editTextPhoto.setText(fileName); 
       checkSelectedItem = true; 

       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
       photo.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos); 
       byte[] bitmapdata = bos.toByteArray(); 

       // Storing Image in new folder 
       StoreByteImage(mContext, bitmapdata, 100, fileName); 

       sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
         Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

       // Delete the image from the Gallery 

       getContentResolver().delete(newuri, null, null); 

      } 
      c1.close(); 

      } 
     } catch (NullPointerException e) { 
      System.out.println("Error in creating Image." + e); 

     } catch (Exception e) { 
      System.out.println("Error in creating Image." + e); 
     } 
     System.out.println("*** End of onActivityResult() ***"); 
    } 

     public String getRealPathFromURI(Uri contentUri) { 
      String[] proj = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
      int column_index = cursor 
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 

     public boolean StoreByteImage(Context pContext, byte[] pImageData, 
       int pQuality, String pExpName) { 

      String nameFile = pExpName; 
      // File mediaFile = null; 
      File sdImageMainDirectory = new File(
        Environment.getExternalStorageDirectory() + "/pix/images"); 
      FileOutputStream fileOutputStream = null; 
      try { 

       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 0; 
       Bitmap myImage = BitmapFactory.decodeByteArray(pImageData, 0, 
         pImageData.length, options); 
       if (!sdImageMainDirectory.exists()) { 
        sdImageMainDirectory.mkdirs(); 
       } 

       sdImageMainDirectory = new File(sdImageMainDirectory, nameFile); 
       sdImageMainDirectory.createNewFile(); 

       fileOutputStream = new FileOutputStream(
         sdImageMainDirectory.toString()); 
       BufferedOutputStream bos = new BufferedOutputStream(
         fileOutputStream); 
       myImage.compress(CompressFormat.JPEG, pQuality, bos); 

       bos.flush(); 
       bos.close(); 

      } catch (FileNotFoundException e) { 
       Toast.makeText(pContext, e.getMessage(), Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       Toast.makeText(pContext, e.getMessage(), Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } 

和ImageView的main.xml中是

<ImageView 
     android:id="@+id/test_image" 
     android:src="@drawable/gray_pic" 
     android:layout_width="180dp" 
     android:layout_height="140dp" 
     android:layout_below="@id/edit2" 
     android:layout_toRightOf="@id/edit3" 
     android:layout_alignParentRight="true" 
     android:layout_marginTop="7dp" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="7dp" 
     android:layout_marginRight="7dp" 
     /> 

有了這個代碼讓我的文件夾中的小尺寸的ImageView和形象店。 如果我添加intent.putExtra,那麼在ImageView中既不會捕獲圖像,也不會在新文件夾中創建圖像。

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
path += "/myFolder/myPicture.jpg"; 
File file = new File(path); 
Uri outputFileUri = Uri.fromFile(file); 
cameraIntent.putExtra("output", outputFileUri); 
startActivityForResult(Intent.createChooser(cameraIntent, "Select Picture"), CAMERA_REQUEST); 
} 

不知道我在哪裏來襲.. 任何關於這幫助將不勝感激。

回答

0

使用相機意圖爲:

Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile()); 
        photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); 
        photoPickerIntent.putExtra("return-data", true); 
        startActivityForResult(Intent.createChooser(photoPickerIntent,"Select Picture"),TAKE_PICTURE); 

// getTempFile()

private Uri getTempFile() { 
    //   if (isSDCARDMounted()) { 

      File root = new File(Environment.getExternalStorageDirectory(), "My Equip"); 
      if (!root.exists()) { 
       root.mkdirs(); 
      } 
      Log.d("filename",filename); 
      File file = new File(root,filename+".jpeg"); 

        muri = Uri.fromFile(file); 
        photopath=muri.getPath(); 
        Item1.photopaths=muri.getPath(); 

      Log.e("getpath",muri.getPath()); 
       return muri; 
    //   } else { 
    //   return null; 
       } 
       //} 
      private boolean isSDCARDMounted(){ 
       String status = Environment.getExternalStorageState(); 
       if (status.equals(Environment.MEDIA_MOUNTED)) 
       return true; 
       else 
       return false; 

       } 

,並檢查您的文件夾,點擊縮略圖,它會顯示實際圖像

+0

感謝ABHI的答覆。但我仍然得到相同的結果。該文件夾本身沒有創建。我的outputFileUri在上面的代碼中是否正確?而且還沒有在imageview中獲取圖像縮略圖...其實我正在使用StoreByteImage()方法來創建文件夾和存儲圖像。該方法的任何更改要完成? – Shrikant 2012-01-28 11:47:22

+0

看到我編輯的問題,試試那種方式 – Abhi 2012-01-28 12:22:29

+0

我會試一試,讓你知道.. – Shrikant 2012-01-28 13:51:44