按名称从SD卡上传Android文件 [英] Uploading android file by name from sd card

查看:144
本文介绍了按名称从SD卡上传Android文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个previous问题(采取screensot和保存的Andr​​oid )我发现了如何将文件保存到Android设备的SD卡,它工作正常。现在我需要知道如何按名称编程找到该文件,然后将该文件上传到Facebook或Twitter。目前我最大的问题是只是为了找到该文件。

In a previous question (Take screensot and save android) I found out how to save a file to the sd card of an android device and it works properly. Now I need to know how to find the file programatically by name, and then upload the file to facebook or twitter. Currently my biggest issue is just to find the file.

推荐答案

要获得图库文件的路径

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。
一般来说上传文件到服务器

To upload file to a server. I am not aware of how to upload file to facebook or twitter. In general to upload file to server

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

这篇关于按名称从SD卡上传Android文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆