如何从DCIM目录中随机选择一张图片并将其设置为壁纸?

huangapple go评论71阅读模式
英文:

how to pick a random image from the DCIM directory to set it as wallpaper?

问题

我在从DCIM目录中随机选择图像方面遇到了问题。

以下是我的代码(位于MainActivity.java中):

public class MainActivity extends AppCompatActivity {

    Random ran;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String secStore = System.getenv("SECONDARY_STORAGE");
        File imagedir = new File(secStore + "/DCIM");
        int totalNumFiles = imagedir.listFiles().length;   // DCIM中的文件数量
        // 随机选择一个
        ran = new Random();
        int indice = ran.nextInt(totalNumFiles) + 1;

        Bitmap bitmap = BitmapFactory.decodeFile(imagedir.listFiles()[indice].getAbsolutePath());
        WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
        try {
            manager.setBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在清单文件中,我已经声明了:

<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

activity_main.xml 文件中没有任何内容。

我的问题:

Bitmap bitmap = BitmapFactory.decodeFile(imagedir.listFiles()[indice].getAbsolutePath());

我无法成功地在随机数和位图之间建立联系。

有人有主意吗?

英文:

I have a problem with picking randomly an image from the DCIM directory.

Here is my code (in MainActivity.java):

public class MainActivity extends AppCompatActivity {

    Random ran;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

                String secStore = System.getenv(&quot;SECONDARY_STORAGE&quot;);
                File imagedir = new File(secStore + &quot;/DCIM&quot;);
                int totalNumFiles = imagedir.listFiles().length;   //nb of files in DCIM
                //we pick up one randomly
                ran = new Random();
                int indice = ran.nextInt(totalNumFiles) + 1;

                Bitmap bitmap = imagedir[indice];
                WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
                manager.setBitmap(bitmap);
//              Toast.makeText(context, &quot;WP set!&quot;, Toast.LENGTH_SHORT).show();
    }

}

and in the manifest, I have declared:

&lt;uses-permission android:name=&quot;android.permission.SET_WALLPAPER&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;

The activity_main.xml has nothing on it.

My problem:

Bitmap bitmap = imagedir[indice];

I can't succeed to make the bridge between the random and the bitmap.

Anybody has an idea?

答案1

得分: 0

现在它可以工作了,我已经找到了解决我的两个问题的方法:

  • minSDK API23 到 API22:
    最简单的方法是从头开始创建一个全新的项目,将最小SDK设置为API22,以摆脱这个问题。然后,Java会指出getExternalStoragePublicDirectory已经不建议使用了,但下面的代码完全可以工作。

  • 正确的代码:

package com.example.randomwallpaper;

import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import static android.os.Environment.getExternalStoragePublicDirectory;

public class MainActivity extends AppCompatActivity {

    Random ran;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String secStore = System.getenv("SECONDARY_STORAGE");
        File imagedir = getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        int totalNumFiles = imagedir.listFiles().length;   // DCIM目录中的文件数
        // 随机选择一个
        ran = new Random();
        int indice = ran.nextInt(totalNumFiles) + 1;

        Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(imagedir.listFiles()[indice]));
        WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
        try {
            manager.setBitmap(bitmap);
            Toast.makeText(getApplicationContext(), "WP set!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_SHORT).show();
        }
    }
}

每次启动应用程序时,壁纸都会随机更改。这些图像存储在DCIM目录中。DCIM目录中只有图像,没有子目录。不要忘记在清单中声明用户的权限。

英文:

So, it works now and I have found a solution to my two problems:

  • minSDK API23 to API22:
    the simplest was to recreate a totally new project with the right minSDK API22 from scratch to get rid of that point. Java indicates then that getExternalStoragePublicDirectory is deprecated but the code below fully works.

  • right code:
    Here it is:


package com.example.randomwallpaper;

import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import static android.os.Environment.getExternalStoragePublicDirectory;

public class MainActivity extends AppCompatActivity {

    Random ran;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String secStore = System.getenv(&quot;SECONDARY_STORAGE&quot;);
        File imagedir = getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        int totalNumFiles = imagedir.listFiles().length;   //nb of files in DCIM
        //we pick up one randomly
        ran = new Random();
        int indice = ran.nextInt(totalNumFiles) + 1;

        Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(imagedir.listFiles()[indice]));
        WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
        try {
            manager.setBitmap(bitmap);
            Toast.makeText(getApplicationContext(), &quot;WP set!&quot;, Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), &quot;Error!&quot;, Toast.LENGTH_SHORT).show();
        }
    }
}

Each time you launch the app, the wallpaper is randomly changed. The images are stored in the DCIM directory. There are only images and no subdirectory in the DCIM directory. Don't forget to declare the user's permissions in the manifest.

huangapple
  • 本文由 发表于 2020年9月23日 01:10:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64014564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定