[英]How to get android lock screen wallpaper?
我使用下面的代碼檢索android 8.1手機上的android鎖屏壁紙:
WallpaperManager manager = WallpaperManager.getInstance(getActivity());
ParcelFileDescriptor pfd = manager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
if (pfd == null) // pfd is always null for FLAG_LOCK, why?
return;
Bitmap lockScreenWallpaper = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
// ...
我已經授予了READ_EXTERNAL_STORAGE
權限並預先設置了鎖定屏幕壁紙。
我在真正的手機上運行演示,發現FLAG_LOCK
的pfd
總是為FLAG_LOCK
,所以我無法獲得鎖屏壁紙。 請幫忙解決問題,謝謝。
我自己找到了答案,我希望它能幫助其他人解決同樣的問題。
getWallpaperFile的官方文檔說: If no lock-specific wallpaper has been configured for the given user, then this method will return null when requesting FLAG_LOCK rather than returning the system wallpaper's image file.
描述含糊不清,至少不夠清楚,這是什么意思? 如果將照片設置為鎖定屏幕和主屏幕壁紙,則兩者共享同一文件,然后通過調用
ParcelFileDescriptor pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
pfd
將永遠為null,那么你應該以這種方式得到鎖屏壁紙:
if (pfd == null)
pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);
你會得到非null的pfd
。 在這種情況下, no lock-specific wallpaper has been configured.
於no lock-specific wallpaper has been configured.
相反,如果您將照片直接設置為鎖定屏幕壁紙, lock-specific wallpaper has been configured
, wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM)
將返回非空值。
所以這是我用來檢索鎖屏壁紙的代碼:
/**
* please check permission outside
* @return Bitmap or Drawable
*/
public static Object getLockScreenWallpaper(Context context)
{
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
if (Build.VERSION.SDK_INT >= 24)
{
ParcelFileDescriptor pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
if (pfd == null)
pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);
if (pfd != null)
{
final Bitmap result = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
try
{
pfd.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}
}
return wallpaperManager.getDrawable();
}
不要忘記在清單文件中添加READ_EXTERNAL_STORAGE
並將其授予外部。
我測試的代碼與您的代碼類似。 我在三星A5和LG Nexus 5X上測試過它。
MainActivity.java
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.WallpaperManager;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CODE_EXTERNAL_STORAGE = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
permission = Manifest.permission.READ_EXTERNAL_STORAGE;
}
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{permission}, REQUEST_CODE_EXTERNAL_STORAGE);
} else {
retrieveLockScreenWallpaper();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_EXTERNAL_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
retrieveLockScreenWallpaper();
}
}
}
}
@SuppressLint("MissingPermission")
private void retrieveLockScreenWallpaper() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
ParcelFileDescriptor descriptor = manager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
if (descriptor != null) {
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(descriptor.getFileDescriptor());
((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);
}
}
}
}
的manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="aminography.com.lockscreenapplication">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
...
</application>
</manifest>
。
關於LGE Nexus 5X(Android 8.1.0,API 27)的結果:
如果沒有為給定用戶配置特定於鎖的壁紙,則此方法將在請求FLAG_LOCK時返回null,而不是返回系統壁紙的圖像文件。
您確定為當前用戶設置了有效的鎖屏壁紙嗎?
/**
* Get an open, readable file descriptor to the given wallpaper image file.
* The caller is responsible for closing the file descriptor when done ingesting the file.
*
* <p>If no lock-specific wallpaper has been configured for the given user, then
* this method will return {@code null} when requesting {@link #FLAG_LOCK} rather than
* returning the system wallpaper's image file.
*
* @param which The wallpaper whose image file is to be retrieved. Must be a single
* defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
* {@link #FLAG_LOCK}.
*
* @see #FLAG_LOCK
* @see #FLAG_SYSTEM
*/
public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which) {
return getWallpaperFile(which, mContext.getUserId());
}
在WallpaperManager openDefaultWallpaper中有一個類似的功能,如果你想要LOCK-SCREEN壁紙你得到null,總是,因為根據代碼工廠默認鎖定壁紙尚不支持。
所以在你的情況下,你可能還沒有設置鎖屏壁紙。 只是為了測試,下載任何壁紙應用程序,並嘗試設置壁紙,然后使用您之前的代碼,以獲得它。
/**
* Open stream representing the default static image wallpaper.
*
* If the device defines no default wallpaper of the requested kind,
* {@code null} is returned.
*
* @hide
*/
public static InputStream openDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
final String whichProp;
final int defaultResId;
if (which == FLAG_LOCK) {
/* Factory-default lock wallpapers are not yet supported
whichProp = PROP_LOCK_WALLPAPER;
defaultResId = com.android.internal.R.drawable.default_lock_wallpaper;
*/
return null;
}
您可以訪問以下網址查看代碼: http : //androidxref.com/8.1.0_r33/xref/frameworks/base/core/java/android/app/WallpaperManager.java#openDefaultWallpaper
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.