Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 应用程序在使用意图捕获图片后崩溃_Android - Fatal编程技术网

Android 应用程序在使用意图捕获图片后崩溃

Android 应用程序在使用意图捕获图片后崩溃,android,Android,我的应用程序在使用intents捕获5到6张照片后崩溃。log cat显示任何内容。我找不到它崩溃的原因。请帮帮我 private void capturePhoto() { File root = new File(Environment.getExternalStorageDirectory(), "Feedback"); if (!root.exists()) { root.mkdirs(); }

我的应用程序在使用intents捕获5到6张照片后崩溃。log cat显示任何内容。我找不到它崩溃的原因。请帮帮我

    private void capturePhoto() {

        File root = new File(Environment.getExternalStorageDirectory(), "Feedback");
        if (!root.exists()) {
            root.mkdirs();
        }
        File file = new File(root, Constants.PROFILE_IMAGE_NAME + ".jpeg");
        Uri outputFileUri = Uri.fromFile(file);


        Intent photoPickerIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        photoPickerIntent.putExtra("return-data", true);
        photoPickerIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
        startActivityForResult(photoPickerIntent, requestCode);


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (this.requestCode == requestCode && resultCode == RESULT_OK) {

            File root = new File(Environment.getExternalStorageDirectory(), "Feedback");
            if (!root.exists()) {
                root.mkdirs();
            }
            File file = new File(root, Constants.PROFILE_IMAGE_NAME+".jpeg");
            checkFlowIdisPresent(file);

            displayPic();


        }
    }
  private void displayPic() {

        String filePath = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + File.separator + "/Feedback/" + Constants.PROFILE_IMAGE_NAME + ".jpeg";
        //  Bitmap bmp = BitmapFactory.decodeFile(filePath);
        //Bitmap scaled = Bitmap.createScaledBitmap(bmp, 300, 300, true);


        File imgFile = new File(filePath);
        Bitmap bmp = decodeFile(imgFile);

        if (imgFile.exists()) {

            dispProfilePic.setImageBitmap(bmp);
        } else {
            dispProfilePic.setBackgroundResource(R.drawable.user_image);

        }
    }

 private Bitmap decodeFile(File f) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE &&
                    o.outHeight / scale / 2 >= REQUIRED_SIZE) {
                scale *= 2;
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
        }
        return null;
    }
上述代码用于捕获照片并在ImageView中显示捕获的图片。我正在使用MI标签

编辑实际上应用程序没有崩溃…它变成了白色屏幕,如果我按下任何按钮,它就会崩溃,并且当它变成白色屏幕时,onActivityResult不会执行

新编辑我能够复制此内容。我点击了安卓显示器,因为我点击了显示器。然后显示我与应用程序交互时应用程序的内存利用率。现在在左侧栏中,我单击了终止应用程序图标。现在有趣的是,它破坏了当前的活动,并转移到以前的活动。之前的活动变成了白色屏幕


请帮帮我,伙计们。

试试这个代码。我在一些应用程序中使用它:

发射意图方法:

private void launchCamera() {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    }
捕获结果:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (requestCode == CAMERA_PIC_REQUEST) {
                if (data != null) {
                    Bundle extras = data.getExtras();
                    if (extras != null) {
                        Bitmap thumbnail = (Bitmap) extras.get("data");
                        if (thumbnail != null)
                            displayPic(thumbnail);
                    }
                }
            }
            } catch (Exception e) {
            e.printStackTrace();
            }
    }

你的代码很好

我认为您保存图像或用相同的名称覆盖同一路径上的图像,因此内存有问题。因此,我建议您使用
System.currentTimeMillis()
或任何随机名称来更改名称,而不是
常量.PROFILE\u IMAGE\u name

并检查权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

发生这种情况的原因可能是,调用活动被杀死,然后由操作系统重新启动,因为图像捕获意图处理通过摄像头捕获的位图所需的大量内存

解决方案:保存图像的文件路径,并在调用onActivityResult时使用它。您可以使用onSavedInstanceStateonRestoreInstanceState方法保存和检索活动的图像路径和其他字段


您可以参考此链接,了解如何使用下面的代码。这对我来说很好

 private static final int REQUEST_CAMERA = 1;

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK)
    {
        if (requestCode == REQUEST_CAMERA)
        {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

            File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");

            FileOutputStream fos;

            try
            {
                destination.createNewFile();
                fos = new FileOutputStream(destination);
                fos.write(bytes.toByteArray());
                fos.close();
            }
            catch (FileNotFoundException fnfe)
            {
                fnfe.printStackTrace();
            }
            catch (IOException ioe)
            {
                ioe.printStackTrace();
            }
            ivSetImage.setImageBitmap(thumbnail);

        }
   }
}
在给定的代码片段中,我压缩了捕获的图像,因此解决了应用程序崩溃问题

在您的情况下,捕获的图像质量可能很高,这是因为您的应用程序在
ImageView
上设置图像时崩溃

试着压缩一个图像。它会成功的

不要忘记在清单文件中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

尝试在异步任务中执行此操作,因为您面临的问题是由UI线程中完成的处理引起的


有关异步任务实施的更多帮助,请参阅检查您的Manifast.xml文件权限 外部存储器

和相机许可



如果您的应用程序在Marsh上运行
在此处输入代码
mallow检查运行时权限

如果log cat上没有显示任何内容,则很难推测任何内容,但请在使用模拟器时检查问题是否,而不是在真实设备上。您还可以通过设置模拟器容量更小(Ram和内部内存)。如果是这种情况,那么增加模拟器的内存或ram,它应该可以正常工作。然后,您需要对低规格设备的图像处理任务进行优化

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
希望这有帮助。

尝试使用以下代码:

private void launchCamera() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (requestCode == CAMERA_PIC_REQUEST) {
            if (data != null) {
                Bundle extras = data.getExtras();
                if (extras != null) {
                    Bitmap thumbnail = (Bitmap) extras.get("data");
                    if (thumbnail != null)
                        displayPic(thumbnail);
                }
            }
        }
        } catch (Exception e) {
        e.printStackTrace();
        }
}

这可能是内存问题,您正在拍摄照片并将其存储在位图中 检查您的android监视器,查看应用程序的内存检测 只需将此方法设置为静态

private static Bitmap decodeFile(File f) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE &&
                    o.outHeight / scale / 2 >= REQUIRED_SIZE) {
                scale *= 2;
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
        }
        return null;
    }

使用不同的名称保存文件,例如使用时间戳作为名称保存文件

您可以发布崩溃日志吗?啊,原木猫什么也没显示它什么也没显示。它很难复制,它必须展示一些东西。一旦你成功复制了它,请在这里发布,因为没有它,几乎不可能帮助您:/n您是否在清单文件中声明了权限,如写入外部存储和camera@NarenderReddy如果没有权限,它将崩溃first timeConstants.PROFILE\u IMAGE\u NAME是随机流请不要发布与您的答案无关的链接!
private static Bitmap decodeFile(File f) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE &&
                    o.outHeight / scale / 2 >= REQUIRED_SIZE) {
                scale *= 2;
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
        }
        return null;
    }