Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.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-ImageView赢得';不显示用相机拍摄的照片_Android_Camera_Imageview - Fatal编程技术网

Android-ImageView赢得';不显示用相机拍摄的照片

Android-ImageView赢得';不显示用相机拍摄的照片,android,camera,imageview,Android,Camera,Imageview,请容忍我。。。我已经四处寻找了天一段工作的、基本的代码,该代码启动相机活动,拍摄一张照片,并将其放在一个简单的ImageView>

请容忍我。。。我已经四处寻找了一段工作的、基本的代码,该代码启动相机活动,拍摄一张照片,并将其放在一个简单的ImageView><下面发布的代码启动活动并拍摄照片,但是图像没有显示在ImageView上缺少什么?:'(

我在三星安卓的一些设备上也遇到了同样的问题,然后我实现了逻辑来获取拍摄照片的路径

尝试添加

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
(还有一点延迟)就在

 if (requestCode == PICK_IMAGE)
阻止。我认为问题可能是您的设备没有正确刷新媒体存储


(当然更好的做法是使用MediaScanner)

尝试并测试了Galaxy S3手机。感谢TGMCians的帮助

public class MainActivity extends Activity
{
private static final int PICK_IMAGE = 0;

private static final int PICK_IMAGE_FROM_GALLERY = 1;

private Button mBtnCamera, mBtnGallery, mBtnCancel;

private ImageView mImageView;

private Uri mURI;
private String mPhotoPath;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mImageView = (ImageView) findViewById(R.id.imgDisplayImage);

    mBtnCamera = (Button) findViewById(R.id.btnPhotoCamera);
    mBtnGallery = (Button) findViewById(R.id.btnPhotoGallery);
    mBtnCancel = (Button) findViewById(R.id.btnCancel);

    mBtnCamera.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent camera = new Intent();

            camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

            camera.putExtra("crop", "true");

            File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

            mURI = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFile.jpg"));

            camera.putExtra(MediaStore.EXTRA_OUTPUT, mURI);

            startActivityForResult(camera, PICK_IMAGE);
        }
    });
}   

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == Activity.RESULT_OK)
    {
        if (requestCode == PICK_IMAGE)
        {
            Cursor cursor = getContentResolver().query(
                    Media.EXTERNAL_CONTENT_URI, new String[]
                            {
                                Media.DATA, 
                                Media.DATE_ADDED, 
                                MediaStore.Images.ImageColumns.ORIENTATION
                            }, 
                            Media.DATE_ADDED, 
                            null, 
                            "date_added ASC"
            );

            if (cursor != null && cursor.moveToFirst())
            {
                do
                {
                    mURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
                    mPhotoPath = mURI.toString();
                }
                while (cursor.moveToNext());
                cursor.close();
            }
            if (data != null)
            {
                if (data.hasExtra("data"))
                {
                    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

                    mImageView.setImageBitmap(thumbnail);
                }
                else
                {
                    System.out.println("Intent bundle does not have the 'data' Extra");

                    int width = mImageView.getWidth();
                    int height = mImageView.getHeight();

                    BitmapFactory.Options factoryOptions = new BitmapFactory.Options();

                    factoryOptions.inJustDecodeBounds = true;

                    BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions);

                    int imageWidth = factoryOptions.outWidth;
                    int imageHeight = factoryOptions.outHeight;

                    // Determine how much to scale down the image
                    int scaleFactor = Math.min(
                            imageWidth/width,
                            imageHeight/height
                            );

                    // Decode the image file into a Bitmap sized to fill view

                    factoryOptions.inJustDecodeBounds = false;
                    factoryOptions.inSampleSize = scaleFactor;
                    factoryOptions.inPurgeable = true;

                    Bitmap bitmap = BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions);

                    mImageView.setImageBitmap(bitmap);

                }
            }
        }
    }
    else 
    {
        System.out.println("Picture taking activity NOT returning RESULT_OK");
    }
}
}

尝试在if条件下提供位图照片=(位图)数据。getExtras().get(“数据”);而不是位图缩略图=数据。getParcelableExtra(“数据”);尝试了,不起作用:(我添加了你的建议,但仍然不起作用,即使合并了你的建议:(我现在将编辑主线程以显示整个代码的外观,太长了,无法将所有内容粘贴到此处。此外,我不是在虚拟机上测试,而是在Galaxy S3手机上测试。我在三星也遇到了同样的问题。请在onActivityResult中发布我的代码,然后您将获得位图的路径。我刚刚完成了。也许我又在某个地方出了问题。请注意r look?:)@Emiliano De Santis请查看我的答案,如果您有任何问题,请告诉我,但请小心执行。
 if (requestCode == PICK_IMAGE)
public class MainActivity extends Activity
{
private static final int PICK_IMAGE = 0;

private static final int PICK_IMAGE_FROM_GALLERY = 1;

private Button mBtnCamera, mBtnGallery, mBtnCancel;

private ImageView mImageView;

private Uri mURI;
private String mPhotoPath;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mImageView = (ImageView) findViewById(R.id.imgDisplayImage);

    mBtnCamera = (Button) findViewById(R.id.btnPhotoCamera);
    mBtnGallery = (Button) findViewById(R.id.btnPhotoGallery);
    mBtnCancel = (Button) findViewById(R.id.btnCancel);

    mBtnCamera.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent camera = new Intent();

            camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

            camera.putExtra("crop", "true");

            File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

            mURI = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFile.jpg"));

            camera.putExtra(MediaStore.EXTRA_OUTPUT, mURI);

            startActivityForResult(camera, PICK_IMAGE);
        }
    });
}   

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == Activity.RESULT_OK)
    {
        if (requestCode == PICK_IMAGE)
        {
            Cursor cursor = getContentResolver().query(
                    Media.EXTERNAL_CONTENT_URI, new String[]
                            {
                                Media.DATA, 
                                Media.DATE_ADDED, 
                                MediaStore.Images.ImageColumns.ORIENTATION
                            }, 
                            Media.DATE_ADDED, 
                            null, 
                            "date_added ASC"
            );

            if (cursor != null && cursor.moveToFirst())
            {
                do
                {
                    mURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
                    mPhotoPath = mURI.toString();
                }
                while (cursor.moveToNext());
                cursor.close();
            }
            if (data != null)
            {
                if (data.hasExtra("data"))
                {
                    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

                    mImageView.setImageBitmap(thumbnail);
                }
                else
                {
                    System.out.println("Intent bundle does not have the 'data' Extra");

                    int width = mImageView.getWidth();
                    int height = mImageView.getHeight();

                    BitmapFactory.Options factoryOptions = new BitmapFactory.Options();

                    factoryOptions.inJustDecodeBounds = true;

                    BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions);

                    int imageWidth = factoryOptions.outWidth;
                    int imageHeight = factoryOptions.outHeight;

                    // Determine how much to scale down the image
                    int scaleFactor = Math.min(
                            imageWidth/width,
                            imageHeight/height
                            );

                    // Decode the image file into a Bitmap sized to fill view

                    factoryOptions.inJustDecodeBounds = false;
                    factoryOptions.inSampleSize = scaleFactor;
                    factoryOptions.inPurgeable = true;

                    Bitmap bitmap = BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions);

                    mImageView.setImageBitmap(bitmap);

                }
            }
        }
    }
    else 
    {
        System.out.println("Picture taking activity NOT returning RESULT_OK");
    }
}
}