android - 从SD卡按名称上传Android文件

标签 android sd-card social-media

在上一个问题( Take screensot and save android )中,我发现了如何将文件保存到 Android 设备的 SD 卡上,并且它可以正常工作。现在我需要知道如何通过名称以编程方式查找文件,然后将文件上传到 facebook 或 twitter。目前我最大的问题就是找到该文件。

最佳答案

从图库中获取文件路径

String filepath; // filepath contains path of the file

public void  getImage()
{
    // To open up a gallery browser
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
    // To handle when an image is selected from the browser, add the following to your Activity
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            // currImageURI is the global variable I'm using to hold the content:// URI of the image
            Uri currImageURI = data.getData();

            File file = new File(getRealPathFromURI(currImageURI));

            if (file.exists())
            {
                filepath=file.getAbsolutePath();
            }
            else
            {
                System.out.println("File Not Found");
            }
        }
    }
}

public String getRealPathFromURI(Uri contentUri)
{
    // can post image
    String [] proj={MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery( contentUri,
        proj, // Which columns to return
        null, // WHERE clause; which rows to return (all rows)
        null, // WHERE clause selection arguments (none)
        null); // Order-by clause (ascending by name)
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

从SD卡获取图像

    File file = new File(android.os.Environment.getExternalStorageDirectory(),"MyDraw");
    if(file.exists())
    {
        File myfile = new File(file.getAbsoluteFile()+File.separator+"name.png");
        String path = myfile.getAbsolutePath();
    }

将文件上传到服务器。我不知道如何将文件上传到 Facebook 或 Twitter。 一般是将文件上传到服务器

    public void upload(String filepath)
    {
      try
      {
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        HttpPost httppost = new HttpPost("youe url");
        File file = new File(filepath);
        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(file, "image/jpeg");
        mpEntity.addPart("userfile", cbFile);
        httppost.setEntity(mpEntity);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
   }

关于android - 从SD卡按名称上传Android文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16571001/

相关文章:

安卓谷歌地图

android - 如果蓝牙设备正在连接/断开连接则广播?

git - 初始化 git 仓库对 linux 根目录的影响 3 :)

facebook - 创建在社交网络上分享时每天更新的网页图像(如屏幕截图)

javascript - Facebook 分享不显示来自开放图元标签的图像

android - LibGDX + gdx-freetype + BlueStacks

java - 为什么我的水平回收 View 不会在 fragment 中滚动

java - 获取保存到android SD卡的文件的路径

安卓保存到SD卡

algorithm - 什么是 "safety variable"?