gpt4 book ai didi

android - 用户使用android上传图片时如何从保管箱获取共享链接?

转载 作者:行者123 更新时间:2023-11-30 01:17:59 26 4
gpt4 key购买 nike

当用户上传图片并且我不想将链接保存到我的数据库时,如何获取共享链接保管箱?现在我的应用程序可以将图像上传到保管箱但无法获取链接..当用户单击保存时,我不想将图像上传到保管箱并将链接保存到我的数据库

这是我的代码

package com.emergency.e_place;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.ProgressListener;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AppKeyPair;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
* Created by Eggy on 5/3/2016.
*/
public class WC_Activity extends AppCompatActivity {
final String TAG = "DEBUG";
private ImageSwitcher imageview;
private int PICK_IMAGE_REQUEST = 1;
public static Button button1;
public static Button button2;
public static Button button3;
public static Button button4;
public static TextView textview;

public static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Emergency_Place";
public static File Dir = new File(path);



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wc);

Toolbar toolbar=(Toolbar)findViewById(R.id.toolbarWC);
setSupportActionBar(toolbar);

//ambil lokasi dari MainActivity
Intent myIntent = getIntent(); // gets the previously created intent
final String Latitude = myIntent.getStringExtra("Latitude"); // will return "FirstKeyValue"
final String Longitude= myIntent.getStringExtra("Longitude"); // will return "SecondKeyValue"
Log.d(TAG, "onLocationChanged: " + Longitude);
//editTextLocation.setText(Longitude+", "+Latitude);
//ambil lokasi dari MainActivity

/*
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//zero can be replaced with any action code

Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);//one can be replaced with any action code
*/
//button4 = (Button) findViewById(R.id.ChooseFile);

AndroidAuthSession session = buildSession();
dropboxAPI = new DropboxAPI<AndroidAuthSession>(session);
Dir.mkdirs();


Button ChooseImage = (Button) findViewById(R.id.ChooseImage);
ChooseImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UploadToDropboxFromSelectedApp("imageWC/"+Latitude+"_"+Longitude);
}
});
}

static DropboxAPI<AndroidAuthSession> dropboxAPI;
private static final String APP_KEY = "idzvjf1ekzxxxx";
private static final String APP_SECRET = "idzvjf1ekzxxxx";
private static final String ACCESSTOKEN = "yxlTAA5L3fAAAAAAAAAAB0L-ptbcKn1Cjc5YxIpUnZcTD6uGq-kCVDJ754MY3ARt";
private DropboxAPI.UploadRequest request;
private AndroidAuthSession buildSession()
{
AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeyPair);
session.setOAuth2AccessToken(ACCESSTOKEN);
return session;
}
static final int UploadFromSelectApp = 9501;
static final int UploadFromFilemanager = 9502;
public static String DropboxUploadPathFrom = "";
public static String DropboxUploadName = "";
public static String DropboxDownloadPathFrom = "";
public static String DropboxDownloadPathTo = "";


private void UploadToDropboxFromSelectedApp (String uploadName)
{
DropboxUploadName = uploadName;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Upload from ..."), UploadFromSelectApp);
}

@Override
protected void onActivityResult(final int requestCode, int resultCode, final Intent intent) {

Uri uri = intent.getData();

try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
Log.d(TAG, "niiih : " + String.valueOf(bitmap));

ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
imageView.getLayoutParams().height = 300;
imageView.getLayoutParams().width = 300;
} catch (IOException e) {
e.printStackTrace();
}

Button Simpan = (Button) findViewById(R.id.btnSimpanWC);
Simpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (requestCode == UploadFromSelectApp)
{
Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
final Uri uri = intent.getData();

DropboxUploadPathFrom = getPath(getApplicationContext(), uri);
if(DropboxUploadPathFrom == null) {
DropboxUploadPathFrom = uri.getPath();
}
Thread th = new Thread(new Runnable(){
public void run() {
try
{
final File file = new File(DropboxUploadPathFrom);
InputStream inputStream = getContentResolver().openInputStream(uri);

dropboxAPI.putFile("/" + DropboxUploadName + file.getName().substring(file.getName().lastIndexOf("."),
file.getName().length()), inputStream, file.length(), null, new ProgressListener(){
@Override
public long progressInterval() {return 100;}
@Override
public void onProgress(long arg0, long arg1){}
});
getMain().runOnUiThread(new Runnable()
{
@Override
public void run()
{
Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {e.printStackTrace();}
}
});
th.start();
}
}
});
}

/*
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
imageview.setImageURI(selectedImage);
}

break;
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
imageview.setImageURI(selectedImage);
}
break;
}
}
*/

public String getPath(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Video.Media.DATA, MediaStore.Audio.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String s = cursor.getString(column_index);
if(s!=null) {
cursor.close();
return s;
}
}
catch(Exception e){}
try {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
String s = cursor.getString(column_index);
if(s!=null) {
cursor.close();
return s;
}
}
catch(Exception e){}
try {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
String s = cursor.getString(column_index);
cursor.close();
return s;
}
finally {
if (cursor != null) {
cursor.close();
}
}
}

public WC_Activity getMain()
{
return this;
}
}

最佳答案

如果我对你的问题的理解正确,那么你正在使用 v1 Android SDK ( https://www.dropbox.com/developers-v1/core/sdks/android ) 并且你想要共享一个文件。

如果是这样,您只需调用dropboxAPI.share 并传入Dropbox 中文件的路径。您将返回一个带有 url 字段的 DropboxLink 对象。

关于android - 用户使用android上传图片时如何从保管箱获取共享链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37550908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com