Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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_Imageview_Gallery_Uri - Fatal编程技术网

Android 在ImageView中放置大图像不工作

Android 在ImageView中放置大图像不工作,android,imageview,gallery,uri,Android,Imageview,Gallery,Uri,我认为我的图像视图有问题。 我创建了一个图库,在这里我可以触摸图像并将其放在下面的ImageView中: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/fonddegrade">

我认为我的图像视图有问题。 我创建了一个图库,在这里我可以触摸图像并将其放在下面的ImageView中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:background="@drawable/fonddegrade">

<Gallery android:layout_height="wrap_content" 
    android:id="@+id/gallery" 
    android:layout_width="fill_parent" />

<ImageView android:layout_below="@+id/gallery"
    android:layout_height="wrap_content" 
    android:id="@+id/laphoto" 
    android:layout_width="wrap_content" 
    android:layout_centerHorizontal="true"/>

这是完美的工作与小图像,但不与大图像(3264*1952)。当我触摸它时(因此,尝试将其放在ImageView中),我出现了一个错误,我的应用程序崩溃。 以下是显示图像的java代码:

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.photo);

            File images; // Trouver le bon endroit de stockage
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
                images = new File("/sdcard/MyPerformance/Photo");
            else
                images = this.getFilesDir();

            images.mkdirs();
            File[] imagelist = images.listFiles(new FilenameFilter(){   
                @Override   
                public boolean accept(File dir, String name)   
                {   
                    return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
                }
            });

            mFiles = new String[imagelist.length];
            for(int i = 0 ; i< imagelist.length; i++)   
            {   
                mFiles[i] = imagelist[i].getAbsolutePath();   
            }
            mUrls = new Uri[mFiles.length];
            for(int i = 0; i < mFiles.length; i++)   
            {   
                mUrls[i] = Uri.parse(mFiles[i]);      
            }

            imgView = (ImageView)findViewById(R.id.laphoto);
            if(mFiles.length != 0)
                imgView.setImageURI(mUrls[0]);

            gallery = (Gallery) findViewById(R.id.gallery);
            gallery.setAdapter(new ImageAdapter(this));

            gallery.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    imgView.setImageURI(mUrls[position]);
                }
            });
    }
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
setContentView(R.layout.photo);
文件图像;//Trouver le bon endroit de stockage
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
images=新文件(“/sdcard/MyPerformance/Photo”);
其他的
images=this.getFilesDir();
images.mkdirs();
File[]imagelist=images.listFiles(新文件名筛选器(){
@凌驾
公共布尔接受(文件目录,字符串名称)
{   
return((name.endsWith(.jpg))| |(name.endsWith(.png));
}
});
mFiles=新字符串[imagelist.length];
对于(int i=0;i
问题可能来自setImageURI(但我不认为这是原因,因为它使用的是小图像),也可能是因为图片的大小

你能给我什么解决办法来解决这个问题? 谢谢你


PS:为什么我的“你好”总是被删除?

你的图像对于Android来说可能太大了,而且内存不足。应用程序的可用内存可能低至16Mb。您的图像拍摄3264*1952*4=~25.5Mb(宽高argB)。因此,最好将图像调整为较小的大小

请参阅:
然后:

最后:

这里有一个bitmap[util类来帮助您处理大型位图

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BitmapUtils {

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

    }

    return inSampleSize;
}


    public static Bitmap decodeSampledBitmapFromResource(String pathToFile,
                int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathToFile, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(pathToFile, options);
    }

}
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
公共类BitmapUtils{
公共静态整数计算示例大小(
BitmapFactory.Options选项、int reqWidth、int reqHeight){
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
//计算高度和宽度与所需高度和宽度的比率
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
//选择最小比率作为采样值,这将保证
//最终图像的两个尺寸均大于或等于
//要求的高度和宽度。
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
公共静态位图解码SampledBitMapFromResource(字符串路径文件,
输入要求宽度,输入要求高度){
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(路径文件,选项);
//计算样本大小
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeFile(路径文件,选项);
}
}
带有:


Glide完成了所有后端工作。

那么问题出在哪里?它会崩溃吗?顺便说一句,你的图像看起来非常大,Android在加载图像时可能会内存不足。预调整图像大小-这是一个很好的例子。商业产品也是,如果你只是在logcat中检查崩溃错误,它会给你一个线索??请这样做,如果仍然存在,请发布错误我有点怀疑。关于你的“你好”,你可以在这里看一下:就是这样!你的链接帮了我很多忙,我的问题与第二个链接相同,我使用了相同的解决方案来解决这个问题。因此,我们必须缩放太大的图像才能很好地工作。虽然这个链接可以回答问题,但在堆栈溢出时不鼓励只回答链接,你可以可以通过获取链接的重要部分并将其放入您的答案中来改进此答案,这样可以确保在链接被更改或删除时,您的答案仍然是答案:)
Glide.with(getContext())
     .load(selectedImageUri)
     .into(imageView);