Android开发问题积累 <加载在线Gif>

时间:2023-03-09 17:40:32
Android开发问题积累   <加载在线Gif><WebView无法加载网页图片>

在线Gif加载

解决办法

Glide完美解决

Glide.with(context).load(pic).placeholder(R.drawable.loading).into(imageView);

pic: url地址

R.drawable.loading: gif加载之前图片

imageView:目标imageview


Intent调用图库

使用L版本手机的时候,通过Intent调用图库,选择图片后出现”不支持的媒体类型”或者是”Failed to read row 0, column 6 from a CursorWindow which has 1 rows, 6 columns”。

由于对数据库这一款疏于了解,不能很快的定位出这个问题,首先想到的办法就是将出问题的文件不要暴露在用户眼前。

解决办法

Intent intent = null;
if (Build.VERSION.SDK_INT < 19)
{
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
}
else
{
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
}

不同的Android版本,选择系统图库中图片的方式有点差别,这个是需要注意的,Google不断更新API肯定是有原因的,所以,Android版本的兼容性问题是迭代开发和维护工作中不可避免的。单看代码是比较简单,但是当开发者不知道的时候,这个问题可能就会浪费很多时间。


AlertDialog获取PositionButton,NeturalButton 和 NegativeButton,一定要先调用show()方法

解决办法

dialog.show();//注意  必须先调用这个,否则下面会报空指针异常
if(xxxxx) { dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(GONE);
} else { dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(VISIBLE);
}

L版本上Toast无法显示

解决办法

  1. L版本上通知管理中,关闭了对应应用的通知权限,去“设置”-> “通知管理”中打开即可
  2. 避免因为权限问题导致Toast无法显示,可以尝试自定义完成类似Toast的功能


TextView走马灯

解决办法

TextView tv = new TextView(activity);
tv.setText("XXXXXXXXXXXX");
tv.setSingleLine(true);
tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
tv.setMarqueeRepeatLimit(-1);
tv.setFocusable(true);
tv.setFocusableInTouchMode(true);
tv.setHorizontallyScrolling(true);
tv.requestFocus();

使用系统ACTION_SEND 发送文字或者图片

解决办法

发送文字

String message = "This is a sharing Text";
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
this.startActivity(shareIntent);

发送图片

/* localqrcode是一个bitmap对象*/
String pathofBmp = Images.Media.insertImage( activity.getContentResolver(), localqrcode,
"title", null);
Uri bmpUri = Uri.parse(pathofBmp);//获取本地Uri
Intent intent= new Intent( android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, bmpUri);//流类型
intent.setType("image/png");
activity.startActivity(intent.createChooser(intent, "选择"));

调用系统资源

解决办法

/*
获取导航栏高度
*/
private int getNavigationBarHeight() {
boolean hasNavigationBar = getResources().getBoolean(Resources.getSystem().getIdentifier("config_showNavigationBar", "bool", "android"));
if(hasNavigationBar) {
return getResources().getDimensionPixelSize(Resources.getSystem().getIdentifier("navigation_bar_height", "dimen","android"));
}
return 0;
}

通过

Resources.getSystem().getIdentifier(“config_showNavigationBar”, “bool”, “android”));

这类方式获取ID,然后通过正常的资源使用方式使用即可

Okhttp异常

Okhttp异常:

System.err: java.lang.IllegalStateException: closed

System.err: at okio.RealBufferedSource.rangeEquals(RealBufferedSourc

System.err: at okio.RealBufferedSource.rangeEquals(RealBufferedSourc

System.err: at okhttp3.internal.Util.bomAwareCharset(Util.java:397)

System.err: at okhttp3.ResponseBody.string(ResponseBody.java:173)

解决办法

Response response = okHttpClient.newCall(request).execute();

这里返回的response一般我们后面就会执行response.body().string(),

但是如果连续调用两次response.body().string(),就会出现如上的错误


actionBar.setDisplayHomeAsUpEnabled(true);后如何修改返回图标

解决办法

actionBar.setHomeAsUpIndicator(R.drawable.__picker_back);


toolbar上menu的标题字体颜色如何修改

解决办法

style.xml文件中添加<item name="actionMenuTextColor">#ffffff</item>


WebView无法显示图片

解决办法

      webView.getSettings().setBlockNetworkImage(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

Unable to execute dex: method ID not in [0, 0xffff]: 65536

解决办法

Eclipse: dex.force.jumbo=true

Android Studio : multiDexEnabled true