从Dropbox下载图片并在android中的一个活动中观看它

时间:2017-08-18 12:53:16

标签: android dropbox

我的Android应用中有3项活动。第一个打开ssh连接,在我的覆盆子pi上它执行不同类型的操作(运行python脚本,将内容上传到dropbox等)。这个过程通常需要15-20秒,当它准备就绪时,第二个活动出现在我点击下载文件的按钮,然后,在第三个活动上,我可以看到我的rpi所做的文件。

我想将最后两个活动合二为一,因此用户无需点击任何内容即可下载他或她的文件。哪种方式最简单?我可以用延迟或线程来解决这个问题,还是只留下asynctask?

该文件先前在与此placePic函数相同的活动中下载,然后我将其调用:

修改

public class dropboxDownloader extends AppCompatActivity implements View.OnClickListener {

String apta;

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

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    if (shouldAskPermissions()) {
        askPermissions();
    }

    AndroidAuthSession session = null;
    try {
        session = buildSession();
    } catch (DropboxException e) {
        e.printStackTrace();
    }
    dropboxAPI = new DropboxAPI<>(session);
    String[] fnames = null;
    DropboxAPI.Entry dirent = null;
    try {
        dirent = dropboxAPI.metadata("/", 1000, null, true, null);
    } catch (DropboxException e) {
        e.printStackTrace();
    }
    ArrayList<DropboxAPI.Entry> files = new ArrayList<DropboxAPI.Entry>();
    ArrayList<String> dir = new ArrayList<String>();


    int i = 0;
    for (DropboxAPI.Entry ent : dirent.contents) {
        files.add(ent);// Add it to the list of thumbs we can choose from
        //dir = new ArrayList<String>();
        dir.add(files.get(i++).path);
    }

    apta = "something";

    Dir.mkdirs();

    final String DropboxDownloadPathTo = path + apta.substring(0, 30);
    final String DropboxDownloadPathFrom = apta;

    Toast.makeText(getApplicationContext(), "Download file ...", Toast.LENGTH_SHORT)
            .show();

    File file = new File(DropboxDownloadPathTo + DropboxDownloadPathFrom
            .substring(DropboxDownloadPathFrom.lastIndexOf('.')));
    if (file.exists()) file.delete();
    try {
        FileOutputStream outputStream = new FileOutputStream(file);
        dropboxDownloader.dropboxAPI.getFile(DropboxDownloadPathFrom, null,
                outputStream, null);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DropboxException e) {
        Toast.makeText(this, "ez a hiba: " + e, Toast.LENGTH_SHORT).show();
    }

    placePic();
}


public void placePic() {

    backToMenu = (Button) findViewById(R.id.button6);
    backToMenu.setOnClickListener(this);
    goToMyPics = (Button) findViewById(R.id.button5);
    goToMyPics.setOnClickListener(this);
    String this_pic = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/aha" + apta;
    Toast.makeText(this, "this_pic: " + this_pic, Toast.LENGTH_LONG).show();
    File f = new File(this_pic);
    if (f.exists() && !f.isDirectory()) {
        Toast.makeText(this, "delay begins", Toast.LENGTH_SHORT).show();
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(dropboxDownloader.this, "delayed?", Toast
                        .LENGTH_SHORT).show();

                if (apta.startsWith("/pi_pic_lc")) {
                    String dedo = Environment.getExternalStorageDirectory().getAbsolutePath() +
                            "/aha/" +
                            apta;
                    Toast.makeText(dropboxDownloader.this, "open this: " + dedo, Toast
                            .LENGTH_SHORT).show();

                    try {
                        ali.setImageURI(Uri.parse(dedo));

                    } catch (Exception e) {
                        Toast.makeText(dropboxDownloader.this, "error: " + e, Toast.LENGTH_SHORT)
                                .show();
                    }
                } else {
                    String kutya = Environment.getExternalStorageDirectory().getAbsolutePath() +
                            "/aha/"
                            + apta;


                    Toast.makeText(dropboxDownloader.this, "open this: " + kutya, Toast
                            .LENGTH_SHORT).show();
                    try {
                        ali.setImageURI(Uri.parse(kutya));
                        PhotoViewAttacher pv = new PhotoViewAttacher(ali);

                    } catch (Exception e) {
                        Toast.makeText(dropboxDownloader.this, "error: " + e, Toast.LENGTH_SHORT)
                                .show();
                    }
                }
            }
        }, 5000);

    } else {
        Toast.makeText(this, "cant get you the file", Toast.LENGTH_SHORT).show();
    }


}
}

提前致谢!

0 个答案:

没有答案