我知道图像的绝对路径(比如说/sdcard/cats.jpg).这个文件的内容是否有任何方法?
实际上在我的代码中我下载了一个图像并将其保存在特定位置.为了在ImageView中设置图像,我使用路径打开文件,获取字节并创建位图,然后在ImageView中设置位图.这是一个非常缓慢的过程,相反,如果我可以获得内容uri,那么我可以很容易地使用该方法 ImageView.setImageUri(uri)
我正在android上构建一个非常基本的图库,它显示了相机文件夹中的最后一个图像,用户可以向左滑动以查看上一个图像.
我使用viewpager完成了一个实现,使用sampleSize完成了一个pagerAdapter来缩放图像.我的问题是,实现无法像默认的图库应用程序一样高效,每次滑动图像时,您必须等待大约200毫秒才能加载下一个图像.所以基本上我对如何实现它的想法并没有成功.
我怎样才能有效地执行此操作,如果可能的话,可以使用允许稍后放大和缩小的实现?
这看起来很有希望,但我不知道如何用文件而不是drawable实现它 http://www.androidviews.net/2012/11/photoview/
编辑:
我已经设法使用photoview(一个被攻击的viewpager)提高了一点速度,但是将文件路径转换为位图或drawables需要花费太多.
我试过了dilix和iDroid Explorer建议的这两种方法:
// Method A
Drawable d = Drawable.createFromPath(imagePath);
imageView.setImageDrawable(d);
// Method B
Bitmap myBitmap = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(myBitmap);
Run Code Online (Sandbox Code Playgroud)
但两者都会产生错误Bitmap too large to be uploaded into a texture.我需要尽快将图像提供给imageView,并以某种方式绕过这个错误,也许我会得到一个不错的速度.现在我正在缩放图像并将它们转换为可绘制的图像,但效率不高.
我的活动总共包含4张图片.它们都与1080x1920设备的分辨率相匹配.当我使用那些通过XML直接加载到我的活动中的图像运行活动时,它在我的Genymotion模拟器中运行得非常慢并且落在真正的Android设备上.
这是设置:
<android.support.design.widget.AppBarLayout
...>
<android.support.design.widget.CollapsingToolbarLayout
...>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
app:layout_collapseMode="parallax">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/shot_header"
android:scaleType="centerCrop" />
</LinearLayout>
<android.support.v7.widget.Toolbar
.../>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
第一张图片位于CollapsingToolbarlayout中.图像的分辨率为1080x649 PNG.
content_activity:
此图像填充父宽度.它的分辨率为1080x772 PNG.
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/main_image"
android:layout_below="@+id/shot_error_field"
android:src="@drawable/forehand_midpng"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:layout_marginTop="15dp"/>
Run Code Online (Sandbox Code Playgroud)
其他2个图像位于LinearLayout中,其分辨率为500x399
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/main_image">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/imageView3"
android:src="@drawable/forehand_mid_wrong"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/imageView4"
android:src="@drawable/forehand_mid_wrong"
android:layout_weight="1"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
总而言之,我有一个带有4个ImageView的活动,填充了适当大小的图像,对于现代Android设备来说应该没问题.问题是由于高内存消耗,此活动运行速度极慢且滞后.
难道我做错了什么?如何进一步优化这些图像?
我查看了其他线程- 内存不足的问题,但似乎没有人提出解决这个问题的方法.
我需要发送一个像我发送字符串"标题"的imageview,但我不能,我怎样才能将主视图中的imageview(R.drawable.image)发送到第二个活动?
谢谢
主要活动
public void NewActivity(View view){
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("title", getString(R.string.title));
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
第二次活动
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.pantalla);
Bundle extras = getIntent().getExtras();
if (extras == null)
{
return;
}
String title = extras.getString("title");
TextView textview = (TextView)findViewById(R.id.titulo);
textview.setText(title);
}
Run Code Online (Sandbox Code Playgroud) 我尝试了几种不同的方法,但图像视图仍然只显示灰色方块。我正在 API 17 上进行测试。
以下是我尝试过但未成功的选项:
将 XML 中的 ImageView 配置为:
固定宽度和高度;宽度和高度为wrap_content;宽度为match_parent,高度为wrap_content
- android:scaleType为“fitXY”和“centerCrop”
- android:将ViewBounds调整为true | 错误的;
尝试过的图像加载方法:
binding.captcha.setImageBitmap (BitmapFactory.decodeFile (imgFile2.getAbsolutePath ()));
Run Code Online (Sandbox Code Playgroud)
还尝试了这个答案/sf/answers/2508156031/
尝试过滑行:
Glide.with(this)
.load (Uri.fromFile (imgFile2))
.skipMemoryCache (true)
.diskCacheStrategy (DiskCacheStrategy.NONE)
.override (150, 150)
.centerCrop ()
.into (binding.captcha);
Run Code Online (Sandbox Code Playgroud)
下面的选项是唯一显示图像(在背景中)的选项,但是我想知道可能会发生什么来阻止我使用默认方法显示图像......
Drawable drawableImage = new BitmapDrawable(BitmapFactory.decodeFile(imgFile2.getAbsolutePath()));
binding.captcha.setBackgroundDrawable(drawableImage);
Run Code Online (Sandbox Code Playgroud)
当前的 XML:
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:id="@+id/captcha"/>
Run Code Online (Sandbox Code Playgroud)