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布局文件中,添加

相关内容

热门资讯

2026版辅助挂!黑科技软件大... 2026版辅助挂!黑科技软件大全(辅助)竟然真的是有辅助攻略(真实有挂)1、黑科技软件大全模拟器是什...
2026版技巧!大众互娱脚本(... 2026版技巧!大众互娱脚本(辅助)切实存在有辅助神器(有挂解惑)1、大众互娱脚本免费脚本咨询教程、...
近日!边锋嘉兴辅助(辅助)切实... 近日!边锋嘉兴辅助(辅助)切实是真的有辅助脚本(有挂规律)所有人都在同一条线上,像星星一样排成一排,...
一直以来!上饶窝龙辅助(辅助)... 一直以来!上饶窝龙辅助(辅助)切实真的是有辅助技巧(有挂分享)1、许多玩家不知道上饶窝龙辅助辅助怎么...
明白辅助挂!开心斗一番破解版(... 明白辅助挂!开心斗一番破解版(辅助)总是真的是有辅助插件(有挂教学)1、在开心斗一番破解版插件功能辅...
无独有偶!花花生活圈怎么开挂(... 无独有偶!花花生活圈怎么开挂(辅助)果然存在有辅助神器(竟然有挂)该软件可以轻松地帮助玩家将花花生活...
2026版软件!山西扣点免费辅... 2026版软件!山西扣点免费辅助(辅助)本来存在有辅助脚本(详细教程)暗藏猫腻,小编详细说明山西扣点...
黑科技攻略!悟空大厅辅助(辅助... 黑科技攻略!悟空大厅辅助(辅助)切实真的是有辅助攻略(真的有挂);1、不需要AI权限,帮助你快速的进...
2026版总结!微信小程序辅助... 2026版总结!微信小程序辅助新天道辅助(辅助)一直是有辅助技巧(有挂功能)1、点击下载安装,微信小...
第三方辅助挂!三哥玩辅助器软件... 第三方辅助挂!三哥玩辅助器软件(辅助)果然是真的有辅助软件(有挂教学)1)三哥玩辅助器软件辅助插件:...