Android: 从公共Google Drive文件夹加载图片到ImageView随机工作
创始人
2024-10-02 11:30:15
0

要从公共Google Drive文件夹加载图片到ImageView,并实现随机工作,可以按照以下步骤进行操作:

  1. 首先,确保已将Google Drive API添加到您的Android项目中。您可以在项目的build.gradle文件中添加以下依赖项:
implementation 'com.google.android.gms:play-services-drive:17.0.0'
implementation 'com.google.android.gms:play-services-auth:19.0.0'
implementation 'com.google.android.gms:play-services-base:17.0.0'
implementation 'com.google.android.gms:play-services-identity:19.0.0'
  1. 在AndroidManifest.xml文件中添加以下权限:

  1. 创建一个名为GoogleDriveService的类,用于处理与Google Drive API的通信。在该类中,您需要添加以下代码:
import android.content.Context;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.Task;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GoogleDriveService {
    private static final String APPLICATION_NAME = "Your Application Name";
    private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
    private static final List SCOPES = Collections.singletonList(DriveScopes.DRIVE_READONLY);
    private static final String FOLDER_ID = "Your Folder ID";

    private final Drive driveService;

    public GoogleDriveService(Context context) {
        GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, SCOPES);
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(context);
        if (account != null) {
            credential.setSelectedAccount(account.getAccount());
        }
        HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
        driveService = new Drive.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME)
                .build();
    }

    public List getImageUrls() {
        List imageUrls = new ArrayList<>();
        try {
            FileList result = driveService.files().list()
                    .setQ("'" + FOLDER_ID + "' in parents")
                    .setFields("files(webViewLink)")
                    .execute();
            List files = result.getFiles();
            if (files != null) {
                for (File file : files) {
                    imageUrls.add(file.getWebViewLink());
                }
            }
        } catch (GoogleJsonResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageUrls;
    }
}
  1. 在您的Activity中,添加以下代码来加载随机图片到ImageView:
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private Button loadButton;
    private GoogleDriveService googleDriveService;

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

        imageView = findViewById(R.id.image_view);
        loadButton = findViewById(R.id.load_button);
        googleDriveService = new GoogleDriveService(this);

        loadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadImageFromDrive();
            }
        });
    }

    private void loadImageFromDrive() {
        List imageUrls = googleDriveService.getImageUrls();
        if (!imageUrls.isEmpty()) {
            String randomImageUrl = getRandomImageUrl(imageUrls);
            Picasso.get().load(randomImageUrl).into(imageView);
        }
    }

    private String getRandomImageUrl(List imageUrls) {
        Random random = new Random();
        int randomIndex = random.nextInt(imageUrls.size());
        return imageUrls.get(randomIndex);
    }
}
  1. 在activity_main.xml布局文件中,添加

相关内容

热门资讯

透视开挂!hhpoker为什么... 透视开挂!hhpoker为什么一直输,hhpoker为什么一直输-原来一直总是有辅助攻略(哔哩哔哩)...
透视了解!有哪些免费的wpk作... 透视了解!有哪些免费的wpk作弊码,wpk软件是正规的吗-都是一直总是有辅助软件(哔哩哔哩)1、辅助...
透视教你!wpk透视辅助下载,... 透视教你!wpk透视辅助下载,微扑克微乐辅助-原来一直总是有辅助攻略(哔哩哔哩)1、完成有辅助插件,...
透视开挂!wepoker破解器... 透视开挂!wepoker破解器有用吗,wepoker透视有用吗-一贯一直总是有辅助攻略(哔哩哔哩)1...
透视专业!wepoker透视脚... 透视专业!wepoker透视脚本安卓,wepoker私人局透视插件-果然真的是有辅助脚本(哔哩哔哩)...
透视揭露!wepoker可以开... 透视揭露!wepoker可以开透视吗,wepoker软件靠谱么-都是真的是有辅助插件(哔哩哔哩)1、...
透视必备!wejoker辅助脚... 透视必备!wejoker辅助脚本,wepoker怎么开辅助-其实真的有辅助教程(哔哩哔哩)透视必备!...
透视总结!wepoker提高好... 透视总结!wepoker提高好牌率,wepokerplus作弊-本来真的有辅助神器(哔哩哔哩)1. ...
透视科普!hh poker辅助... 透视科普!hh poker辅助器先试用,hhpoker可以控制吗-本来真的是有辅助软件(哔哩哔哩)一...
透视开挂!aapoker透视插... 透视开挂!aapoker透视插件,aapoker怎么设置抽水-竟然一直都是有辅助方法(哔哩哔哩)1、...