【问题标题】:Uploading android file by name from sd card从 sd 卡按名称上传 android 文件
【发布时间】:2013-07-21 19:37:46
【问题描述】:

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

【问题讨论】:

    标签: android sd-card social-media


    【解决方案1】:

    从图库中获取文件路径

    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);
    }
    

    从 sdcard 获取图片

        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();
          }
       }
    

    【讨论】:

    • 我需要知道整个路径和一切吗?还是我只需要知道用户为图片命名的内容?
    • @James 您需要文件的路径才能将图像上传到服务器。该路径将为您提供文件表单的位置,您可以上传与从桌面上传图片相同的内容。
    • @James 的代码发布在上面的答案中。你试过上面的吗??
    • 我现在正在尝试,但我知道可能是什么问题。它检查是否存在文件并获取文件,但那里可能有 2 张或更多图片
    • @rptwsthi 你用什么IDE。如果我包括所有进口,那么帖子将是巨大的。它是不必要的,你应该知道要导入什么。这是基本的。如果您使用 Eclipse,请右键单击 --> 源 --> 组织导入并修复导入或 ctr+shift+o
    【解决方案2】:

    首先您需要知道图片的路径,然后使用 facebook sdk 将图片上传到个人资料中

    https://developers.facebook.com/android/

    在 sdk 中有一个上传图片的例子

    【讨论】:

    • @james 你需要从图库中获取图片吗?或者您需要从 sdcard 获取图像,以防您知道文件名
    • @James 让用户从图库中选择您可以使用代码的第一部分。它会给你选择图像的路径。如果您知道 sdcard 中文件的名称,则可以使用第二部分获取路径。一般来说,要将文件上传到服务器,您可以使用 http post request
    猜你喜欢
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2012-10-08
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多