2016-05-18 4 views
0
private void getDocument() 
{ 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("application/msword,application/pdf"); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 
    // Only the system receives the ACTION_OPEN_DOCUMENT, so no need to test. 
    startActivityForResult(intent, REQUEST_CODE_DOC); 
} 

@Override 
protected void onActivityResult(int req, int result, Intent data) 
{ 
    // TODO Auto-generated method stub 
    super.onActivityResult(req, result, data); 
    if (result == RESULT_OK) 
    { 
     Uri fileuri = data.getData(); 
     docFilePath = getFileNameByUri(this, fileuri); 
    } 
} 

// get file path 

private String getFileNameByUri(Context context, Uri uri) 
{ 
    String filepath = "";//default fileName 
    //Uri filePathUri = uri; 
    File file; 
    if (uri.getScheme().toString().compareTo("content") == 0) 
    { 
     Cursor cursor = context.getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA, MediaStore.Images.Media.ORIENTATION }, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 

     cursor.moveToFirst(); 

     String mImagePath = cursor.getString(column_index); 
     cursor.close(); 
     filepath = mImagePath; 

    } 
    else if (uri.getScheme().compareTo("file") == 0) 
    { 
     try 
     { 
      file = new File(new URI(uri.toString())); 
      if (file.exists()) 
       filepath = file.getAbsolutePath(); 

     } 
     catch (URISyntaxException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    else 
    { 
     filepath = uri.getPath(); 
    } 
    return filepath; 
} 

私は自分のCV(PDFフォームまたはワードフォーム)をアップロードしてからサーバーに送信できるAndroidモバイルアプリケーションを作成しています。AndroidでPDFファイルまたはワードファイルを参照してアップロードする

私はこのコードを使用してPDFやワードファイルを添付/アップロードしていますが、Androidスタジオでアプリケーションを実行すると添付ファイルボタンをクリックできません。どうすれば解決できますか?

+0

AFAIKでは、Androidはカンマ区切りのMIMEタイプのリストをサポートしていません。任意の 'Uri'に対して' DATA'を得ることはできません。また、コードには「添付ファイル」ボタンはありません。 – CommonsWare

+0

私はAndroidが初めてです:(、どうすればこのコードを修正できますか? これはクラス内のコード全体です – 7in7un

答えて

0
public class RegisterActivity extends AppCompatActivity { 
    ImageButton btnAttach; 

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

     getSupportActionBar().setTitle("Registeration"); 

     btnAttach = (ImageButton) findViewById(R.id.attachImageButton); 

     // view products click event 
     btnAttach.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       // Launching All products Activity 
       Intent i = new Intent(getApplicationContext(), RegisterActivity.class); 
       startActivity(i); 

      } 
     }); 
    } 


    public void attachImageButton_OnClick(View view) { 
     Intent intent = new Intent(this, RegisterActivity.class); 
     startActivity(intent); 
    } 


    private void getDocument() 
    { 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.setType("application/msword,application/pdf"); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     // Only the system receives the ACTION_OPEN_DOCUMENT, so no need to test. 
//  startActivityForResult(intent, REQUEST_CODE_DOC); 
    } 


    @Override 
    protected void onActivityResult(int req, int result, Intent data) 
    { 
     // TODO Auto-generated method stub 
     super.onActivityResult(req, result, data); 
     if (result == RESULT_OK) 
     { 
      Uri fileuri = data.getData(); 
     // docFilePath = getFileNameByUri(this, fileuri); 
     } 
    } 

// get file path 

    private String getFileNameByUri(Context context, Uri uri) 
    { 
     String filepath = "";//default fileName 
     //Uri filePathUri = uri; 
     File file; 
     if (uri.getScheme().toString().compareTo("content") == 0) 
     { 
      Cursor cursor = context.getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA, MediaStore.Images.Media.ORIENTATION }, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 

      cursor.moveToFirst(); 

      String mImagePath = cursor.getString(column_index); 
      cursor.close(); 
      filepath = mImagePath; 

     } 
     else 
     if (uri.getScheme().compareTo("file") == 0) 
     { 
      try 
      { 
       file = new File(new URI(uri.toString())); 
       if (file.exists()) 
        filepath = file.getAbsolutePath(); 

      } 
      catch (URISyntaxException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     else 
     { 
      filepath = uri.getPath(); 
     } 
     return filepath; 
    } 
} 
関連する問題