[Android] - Intent.ACTION_GET_CONTENT FileName 찾기

Android Java 2018. 3. 27. 17:06
반응형

======= 호출 =======


Intent intent = new Intent();

intent.setType( "image/*" );

intent.setAction( Intent.ACTION_GET_CONTENT );


startActivityForResult( intent, 1 );


======= 응답 =======

@Override

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    Uri tempUri   = data.getData();

    String result = "";


    if( tempUri.getScheme().equals( "content" ) ) {

      Cursor cursor = getContentResolver().query( tempUri, null, null, null, null );

      try {

        if (cursor != null && cursor.moveToFirst()) {

          result = cursor.getString(cursor.getColumnIndex( OpenableColumns.DISPLAY_NAME ) );

        }

      } finally {

        cursor.close();

      }

    }


    if( result == null ) {

      result  = tempUri.getPath();

      int cut = result.lastIndexOf( "/" );

  

      if( cut != -1 ) {

        result = result.substring( cut + 1 );

      }

    }


Log.e( "TAG", "FileName : " + result );

}

반응형
: