相机捕获的图像未以全尺寸存储

发布于 2024-12-29 14:16:05 字数 5593 浏览 2 评论 0原文

我知道这个问题在这个论坛上已经被问过很多次了。但我仍然无法得到解决方案。 基本上在我的应用程序中,我调用内置相机意图,捕获图像并在 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();
            }

,main.xml 中的 ImageView 是

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

不知道哪里被击中了。。 对此的任何帮助将不胜感激。

I know this question has been asked so many times in this forum. But still I couldn't get the solution.
Basically in my application, I am calling an inbuilt camera intent, capturing image and displaying a bitmap in imageview and storing it in sd card. Now the image what i get in my folder is of small size like a thumbnail.

My code is

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

and ImageView in main.xml is

    <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"
        />

With this code i get an Imageview and the image stores in my folder with small size.
If I add intent.putExtra then neither image captured displays in ImageView nor image creates in new folder.

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

Don't know where I am struck..
Any help on this would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2025-01-05 14:16:05

使用相机意图:

 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;

               }

并检查您的文件夹,单击缩略图它将显示实际图像

Use Camera intent as:

 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;

               }

And Check in your Folder, click on thumbnail it will show actual image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文