我onActivityResult
从mediastore图像选择返回,我可以使用以下内容获取图像的URI:
Uri selectedImage = data.getData();
Run Code Online (Sandbox Code Playgroud)
将其转换为字符串可以得到:
content://media/external/images/media/47
Run Code Online (Sandbox Code Playgroud)
或者路径给出:
/external/images/media/47
Run Code Online (Sandbox Code Playgroud)
但是我似乎无法找到将其转换为绝对路径的方法,因为我想将图像加载到位图而不必将其复制到某处.我知道这可以使用URI和内容解析器完成,但这似乎在重新启动手机时中断,我想MediaStore
在重新启动之间不会保持其编号相同.
在我的Android应用程序中,它自动聚焦我的布局中的第一个按钮,给它一个橙色轮廓.如何最好用XML设置初始焦点,这可以设置为空吗?
我的程序中有一个广播接收器,可以像这样对电池电量做出反应:
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
int level = intent.getIntExtra("level", 0);
// do something...
}
}
registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Run Code Online (Sandbox Code Playgroud)
但是,此代码必须等待更新电池状态,因此如果您有一个需要根据电池电量设置的GUI元素,则必须等待电池事件发生.有没有办法轻推它以使其正常工作或只是运行一些代码来查看最后一次广播的电池电量是多少?
我正在从像这样的资源加载位图:
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image);
Run Code Online (Sandbox Code Playgroud)
我想要做的是在我的绘制方法中将它绘制到主画布之前对位图进行一些更改(因为当它不会改变时,在我的主循环中重复大量绘制似乎很浪费).我正在使用以下内容对位图进行更改:
Canvas c = new Canvas(mBackground);
c.drawARGB(...); // etc
Run Code Online (Sandbox Code Playgroud)
所以我自然会得到一个例外
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
Run Code Online (Sandbox Code Playgroud)
所以为了避免这种情况,我制作了位图的副本,以便它是可变的
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image).copy(Bitmap.Config.ARGB_8888, true);
Run Code Online (Sandbox Code Playgroud)
哪个避免了问题,但它有时会导致OutOfMemoryExceptions,知道更好的方法来实现我想要的东西吗?
相当简单的问题.是否可以注意到Android模拟器中的振动?
在javascript中,以下工作可以将焦点放在edit_2输入框中:
document.getElementById("edit_2").focus();
Run Code Online (Sandbox Code Playgroud)
但是使用Jquery不会:
$("#edit_2").focus;
Run Code Online (Sandbox Code Playgroud) 当突出显示或按下按钮时,我想将按钮内的文本更改为粗体.我目前使用xml文件来定义按钮并使用XML来更改按下时的外观,但我想在不使用图像的情况下执行此操作.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/reset_hover" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/reset_hover" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/reset_hover" />
<item android:drawable="@drawable/reset" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我尝试使用类似下面的内容,但它似乎永远不会被调用.
final Button btn_reset = (Button) findViewById(R.id.btn_reset);
btn_reset.setOnClickListener(this);
btn_reset.setOn(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){btn_reset.setTypeface(null, Typeface.BOLD);}
else{btn_reset.setTypeface(null, Typeface.NORMAL);}
}
});
Run Code Online (Sandbox Code Playgroud) 我有一个使用SD卡上的文件的应用程序,应用程序在手机启动时运行,并且很明显,当程序首次运行时无法访问该文件,因为它在SD卡可用之前开始工作.
是否有一个广播接收器,我可以用来判断SD卡准备好了吗?
更新
只是为了总结注册意图的答案:
IntentFilter filter = new IntentFilter (Intent.ACTION_MEDIA_MOUNTED);
filter.addDataScheme("file");
registerReceiver(this.mSDInfoReceiver, new IntentFilter(filter));
Run Code Online (Sandbox Code Playgroud)
并创建一个广播接收器来响应它:
private BroadcastReceiver mSDInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
// Code to react to SD mounted goes here
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个服务,它会创建一个通知,然后定期用某些信息更新它.电话崩溃并重新启动大约12分钟后,我认为这是由于以下代码中的内存泄漏导致我如何更新通知,有人请检查/建议我是否是这种情况和我我做错了.
的onCreate:
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Run Code Online (Sandbox Code Playgroud)
createNotification:
private void createNotification() {
Intent contentIntent = new Intent(this,MainScreen.class);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent appIntent =PendingIntent.getActivity(this,0, contentIntent, 0);
contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.text, "");
notification = new Notification();
notification.when=System.currentTimeMillis();
notification.contentView = contentView;
notification.contentIntent = appIntent;
}
Run Code Online (Sandbox Code Playgroud)
updateNotification:
private void updateNotification(String text){
contentView.setTextViewText(R.id.text, text);
mNotificationManager.notify(0, notification);
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
保存文件:
FileOutputStream fo = null;
try {
fo = this.openFileOutput("test.png", Context.MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(CompressFormat.PNG, 100, fo)
Run Code Online (Sandbox Code Playgroud)
加载文件:
String fname = this.getFilesDir().getAbsolutePath()+"/test.png";
Bitmap bMap = BitmapFactory.decodeFile(fname);
i.setImageBitmap(bMap);
Run Code Online (Sandbox Code Playgroud)
最后一行给出了空指针异常,为什么BitmapFactory.decodeFile返回null?我可以验证文件是否正确保存,因为我可以使用adb将其拉出来并看到png正确显示.
android ×9
bitmap ×2
battery ×1
focus ×1
imageview ×1
javascript ×1
jquery ×1
mediastore ×1
memory-leaks ×1
mutable ×1
sd-card ×1
uri ×1